SkyNetWalker commited on
Commit
4ca2388
·
verified ·
1 Parent(s): 19bd564

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -25
app.py CHANGED
@@ -16,17 +16,29 @@ client = OpenAI(
16
 
17
  print("Client initialized.")
18
 
 
 
 
 
 
 
 
19
  def respond(
20
  message,
21
  history: list[tuple[str, str]],
22
- system_message,
 
23
  max_tokens,
24
  temperature,
25
  top_p,
26
- model_name, # New parameter for model selection
27
  ):
28
  print(f"Received message: {message}")
29
  print(f"History: {history}")
 
 
 
 
30
  print(f"System message: {system_message}")
31
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
32
  print(f"Selected model: {model_name}")
@@ -47,7 +59,7 @@ def respond(
47
  print("Sending request to OpenAI API.")
48
 
49
  for message in client.chat.completions.create(
50
- model=model_name, # Use the selected model
51
  max_tokens=max_tokens,
52
  stream=True,
53
  temperature=temperature,
@@ -60,40 +72,68 @@ def respond(
60
  yield response
61
 
62
  print("Completed response generation.")
63
-
64
  chatbot = gr.Chatbot(height=400)
65
 
66
  print("Chatbot interface created.")
67
 
68
- # Define the list of models
69
  models = [
70
- "PowerInfer/SmallThinker-3B-Preview", #OK
71
- "Qwen/QwQ-32B-Preview", #OK
72
- "Qwen/Qwen2.5-Coder-32B-Instruct", #OK
73
- "meta-llama/Llama-3.2-3B-Instruct", #OK
74
- #"Qwen/Qwen2.5-32B-Instruct", #fail, too large
75
- "microsoft/Phi-3-mini-128k-instruct", #fail
76
- #"microsoft/Phi-3-medium-128k-instruct", #fail
77
- #"microsoft/phi-4", #fail, too large to be loaded automatically (29GB > 10GB)
78
- #"meta-llama/Llama-3.3-70B-Instruct", #fail, need HF Pro subscription
79
  ]
80
 
81
- # Add a title and move the model dropdown to the top
82
  with gr.Blocks() as demo:
83
- gr.Markdown("# LLM Test") # Add a title to the top of the UI
84
 
85
- # Add the model dropdown above the chatbot
86
- model_dropdown = gr.Dropdown(choices=models, value=models[0], label="Select Model:")
 
 
 
 
 
87
 
88
- # Use the existing ChatInterface
89
- gr.ChatInterface(
90
  respond,
91
  additional_inputs=[
92
- gr.Textbox(value="", label="Additional System Prompt:"),
93
- gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens:"),
94
- gr.Slider(minimum=0.1, maximum=1.0, value=0.3, step=0.1, label="Temperature:"),
95
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P:"),
96
- model_dropdown, # Pass the dropdown as an additional input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  ],
98
  fill_height=True,
99
  chatbot=chatbot,
 
16
 
17
  print("Client initialized.")
18
 
19
+ # Pre-set system prompts
20
+ SYSTEM_PROMPTS = {
21
+ "General Assistant": "You are a helpful, respectful and honest assistant. Always provide accurate information and admit when you're not sure about something.",
22
+ "Code Helper": "You are a programming assistant. Help users with coding questions, debugging, and best practices. Provide clear explanations and code examples when appropriate.",
23
+ "Creative Writer": "You are a creative writing assistant. Help users with storytelling, character development, and creative writing techniques. Be imaginative and encouraging."
24
+ }
25
+
26
  def respond(
27
  message,
28
  history: list[tuple[str, str]],
29
+ preset_prompt,
30
+ custom_prompt,
31
  max_tokens,
32
  temperature,
33
  top_p,
34
+ model_name,
35
  ):
36
  print(f"Received message: {message}")
37
  print(f"History: {history}")
38
+
39
+ # Use custom prompt if provided, otherwise use selected preset
40
+ system_message = custom_prompt if custom_prompt.strip() else SYSTEM_PROMPTS[preset_prompt]
41
+
42
  print(f"System message: {system_message}")
43
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
44
  print(f"Selected model: {model_name}")
 
59
  print("Sending request to OpenAI API.")
60
 
61
  for message in client.chat.completions.create(
62
+ model=model_name,
63
  max_tokens=max_tokens,
64
  stream=True,
65
  temperature=temperature,
 
72
  yield response
73
 
74
  print("Completed response generation.")
75
+
76
  chatbot = gr.Chatbot(height=400)
77
 
78
  print("Chatbot interface created.")
79
 
 
80
  models = [
81
+ "PowerInfer/SmallThinker-3B-Preview",
82
+ "Qwen/QwQ-32B-Preview",
83
+ "Qwen/Qwen2.5-Coder-32B-Instruct",
84
+ "meta-llama/Llama-3.2-3B-Instruct",
85
+ "microsoft/Phi-3-mini-128k-instruct",
 
 
 
 
86
  ]
87
 
 
88
  with gr.Blocks() as demo:
89
+ gr.Markdown("# LLM Test")
90
 
91
+ with gr.Row():
92
+ # Model selection at the top
93
+ model_dropdown = gr.Dropdown(
94
+ choices=models,
95
+ value=models[0],
96
+ label="Select Model:"
97
+ )
98
 
99
+ # Chatbot interface
100
+ chat_interface = gr.ChatInterface(
101
  respond,
102
  additional_inputs=[
103
+ # Prompt selections
104
+ gr.Dropdown(
105
+ choices=list(SYSTEM_PROMPTS.keys()),
106
+ value=list(SYSTEM_PROMPTS.keys())[0],
107
+ label="Select System Prompt:"
108
+ ),
109
+ gr.Textbox(
110
+ value="",
111
+ label="Custom System Prompt (leaves blank to use preset):",
112
+ lines=2
113
+ ),
114
+ # Other parameters
115
+ gr.Slider(
116
+ minimum=1,
117
+ maximum=4096,
118
+ value=1024,
119
+ step=1,
120
+ label="Max new tokens:"
121
+ ),
122
+ gr.Slider(
123
+ minimum=0.1,
124
+ maximum=1.0,
125
+ value=0.3,
126
+ step=0.1,
127
+ label="Temperature:"
128
+ ),
129
+ gr.Slider(
130
+ minimum=0.1,
131
+ maximum=1.0,
132
+ value=0.95,
133
+ step=0.05,
134
+ label="Top-P:"
135
+ ),
136
+ model_dropdown,
137
  ],
138
  fill_height=True,
139
  chatbot=chatbot,