AISparking commited on
Commit
fd32a0e
·
verified ·
1 Parent(s): 5a13fd6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +198 -0
app.py ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #refer llama recipes for more info https://github.com/huggingface/huggingface-llama-recipes/blob/main/inference-api.ipynb
2
+ #huggingface-llama-recipes : https://github.com/huggingface/huggingface-llama-recipes/tree/main
3
+
4
+ import gradio as gr
5
+ from openai import OpenAI
6
+ import os
7
+
8
+ ACCESS_TOKEN = os.getenv("myHFtoken")
9
+
10
+ print("Access token loaded.")
11
+
12
+ client = OpenAI(
13
+ base_url="https://api-inference.huggingface.co/v1/",
14
+ api_key=ACCESS_TOKEN,
15
+ )
16
+
17
+ print("Client initialized.")
18
+
19
+ SYSTEM_PROMPTS = {
20
+ "zh-HK": "用香港的廣東話(Cantonese)對話. No chatty. Answer in simple but accurate way.",
21
+ "zh-TW": "Chat by Traditional Chinese language of Taiwan (zh-TW). No chatty. Answer in simple but accurate way.",
22
+ "EN: General Assistant": "You are a helpful, respectful and honest assistant. Always provide accurate information and admit when you're not sure about something.",
23
+ "EN: Code Helper": "You are a programming assistant. Help users with coding questions, debugging, and best practices. Provide clear explanations and code examples when appropriate.",
24
+ "EN: Creative Writer": "You are a creative writing assistant. Help users with storytelling, character development, and creative writing techniques. Be imaginative and encouraging."
25
+ }
26
+
27
+ def respond(
28
+ message,
29
+ history: list[tuple[str, str]],
30
+ preset_prompt,
31
+ custom_prompt,
32
+ max_tokens,
33
+ temperature,
34
+ top_p,
35
+ model_name,
36
+ ):
37
+ print(f"Received message: {message}")
38
+ print(f"History: {history}")
39
+
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}")
45
+
46
+ messages = [{"role": "system", "content": system_message}]
47
+
48
+ for val in history:
49
+ if val[0]:
50
+ messages.append({"role": "user", "content": val[0]})
51
+ print(f"Added user message to context: {val[0]}")
52
+ if val[1]:
53
+ messages.append({"role": "assistant", "content": val[1]})
54
+ print(f"Added assistant message to context: {val[1]}")
55
+
56
+ messages.append({"role": "user", "content": message})
57
+
58
+ response = ""
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,
66
+ top_p=top_p,
67
+ messages=messages,
68
+ ):
69
+ token = message.choices[0].delta.content
70
+ print(f"Received token: {token}")
71
+ response += token
72
+ yield response
73
+
74
+ print("Completed response generation.")
75
+
76
+ models = [
77
+ "PowerInfer/SmallThinker-3B-Preview",
78
+ "Qwen/QwQ-32B-Preview",
79
+ "Qwen/Qwen2.5-Coder-32B-Instruct",
80
+ "meta-llama/Llama-3.2-3B-Instruct",
81
+ "microsoft/Phi-3-mini-128k-instruct",
82
+ ]
83
+
84
+ with gr.Blocks(css=".main-container {max-width: 900px; margin: auto;}") as demo:
85
+ # Add the banner image
86
+ with gr.Row():
87
+ gr.Image("banner.png", elem_id="banner-image", show_label=False).style(height=200)
88
+
89
+ # Title and description
90
+ gr.Markdown(
91
+ """
92
+ # 🧠 LLM Test Platform
93
+ Welcome to the **LLM Test Platform**! Use this interface to interact with various AI language models.
94
+ Configure the settings, provide your input, and explore the capabilities of state-of-the-art models.
95
+ """,
96
+ elem_id="title",
97
+ )
98
+
99
+ with gr.Row():
100
+ model_dropdown = gr.Dropdown(
101
+ choices=models,
102
+ value=models[0],
103
+ label="**Select Model:**",
104
+ elem_id="model-dropdown"
105
+ )
106
+
107
+ # Create the chat components in a card-style layout
108
+ with gr.Row():
109
+ with gr.Column():
110
+ chatbot = gr.Chatbot(height=500, elem_id="chatbot").style(container=True)
111
+ with gr.Column(scale=1):
112
+ msg = gr.Textbox(
113
+ show_label=False,
114
+ placeholder="Type your message here...",
115
+ container=False,
116
+ elem_id="input-box"
117
+ )
118
+ clear = gr.Button("Clear", elem_id="clear-button")
119
+
120
+ # Additional configuration inputs in an accordion
121
+ with gr.Accordion("⚙️ Configuration", open=False):
122
+ preset_prompt = gr.Dropdown(
123
+ choices=list(SYSTEM_PROMPTS.keys()),
124
+ value=list(SYSTEM_PROMPTS.keys())[0],
125
+ label="**Select System Prompt:**",
126
+ )
127
+ custom_prompt = gr.Textbox(
128
+ value="",
129
+ label="**Custom System Prompt (leave blank to use preset):**",
130
+ lines=2
131
+ )
132
+ max_tokens = gr.Slider(
133
+ minimum=1,
134
+ maximum=8192,
135
+ value=2048,
136
+ step=1,
137
+ label="**Max new tokens:**"
138
+ )
139
+ temperature = gr.Slider(
140
+ minimum=0.1,
141
+ maximum=1.0,
142
+ value=0.3,
143
+ step=0.1,
144
+ label="**Temperature:**"
145
+ )
146
+ top_p = gr.Slider(
147
+ minimum=0.1,
148
+ maximum=1.0,
149
+ value=0.95,
150
+ step=0.05,
151
+ label="**Top-P:**"
152
+ )
153
+
154
+ # Set up the chat functionality
155
+ def user(user_message, history):
156
+ return "", history + [[user_message, None]]
157
+
158
+ def bot(
159
+ history,
160
+ preset_prompt,
161
+ custom_prompt,
162
+ max_tokens,
163
+ temperature,
164
+ top_p,
165
+ model_name
166
+ ):
167
+ history[-1][1] = ""
168
+ for character in respond(
169
+ history[-1][0],
170
+ history[:-1],
171
+ preset_prompt,
172
+ custom_prompt,
173
+ max_tokens,
174
+ temperature,
175
+ top_p,
176
+ model_name
177
+ ):
178
+ history[-1][1] = character
179
+ yield history
180
+
181
+ msg.submit(
182
+ user,
183
+ [msg, chatbot],
184
+ [msg, chatbot],
185
+ queue=False
186
+ ).then(
187
+ bot,
188
+ [chatbot, preset_prompt, custom_prompt, max_tokens, temperature, top_p, model_dropdown],
189
+ chatbot
190
+ )
191
+
192
+ clear.click(lambda: None, None, chatbot, queue=False)
193
+
194
+ print("Gradio interface initialized.")
195
+
196
+ if __name__ == "__main__":
197
+ print("Launching the demo application.")
198
+ demo.launch()