wassemgtk commited on
Commit
0f4d5f6
Β·
verified Β·
1 Parent(s): 59cc454

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -52
app.py CHANGED
@@ -5,14 +5,14 @@ import json
5
 
6
  # These will be set as Hugging Face Spaces secrets
7
  API_KEY = os.environ.get("FIREWORKS_API_KEY", "")
8
- SYSTEM_PROMPT = os.environ.get("SYSTEM_PROMPT", "You are a helpful AI assistant.")
9
 
10
  # API endpoint
11
  API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
12
 
13
  def chat_with_model(message, history, temperature, max_tokens, top_p, top_k):
14
  """
15
- Send a message to the Fireworks AI API and return the response
16
  """
17
  # Build conversation history
18
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
@@ -27,7 +27,7 @@ def chat_with_model(message, history, temperature, max_tokens, top_p, top_k):
27
 
28
  # Prepare the request
29
  headers = {
30
- "Accept": "application/json",
31
  "Content-Type": "application/json",
32
  "Authorization": f"Bearer {API_KEY}"
33
  }
@@ -40,83 +40,115 @@ def chat_with_model(message, history, temperature, max_tokens, top_p, top_k):
40
  "presence_penalty": 0,
41
  "frequency_penalty": 0,
42
  "temperature": temperature,
43
- "messages": messages
 
44
  }
45
 
46
  try:
47
- response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
48
  response.raise_for_status()
49
 
50
- result = response.json()
51
- assistant_message = result["choices"][0]["message"]["content"]
52
- return assistant_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  except requests.exceptions.RequestException as e:
55
- return f"❌ Error: {str(e)}\n\nPlease check your API key in Hugging Face Spaces secrets."
56
- except (KeyError, IndexError) as e:
57
- return f"❌ Error parsing response: {str(e)}"
58
 
59
  # Custom CSS for a modern look
60
  custom_css = """
61
  .gradio-container {
62
  font-family: 'Inter', sans-serif;
 
63
  }
64
  #title {
65
  text-align: center;
66
- background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
67
  -webkit-background-clip: text;
68
  -webkit-text-fill-color: transparent;
69
- font-size: 2.5em;
70
- font-weight: bold;
71
- margin-bottom: 0.5em;
 
72
  }
73
- #description {
74
  text-align: center;
75
- font-size: 1.1em;
76
- color: #666;
77
  margin-bottom: 2em;
 
78
  }
79
  .message-wrap {
80
- border-radius: 12px !important;
 
 
 
81
  }
82
  """
83
 
84
  # Create Gradio interface
85
- with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
86
- gr.HTML("<h1 id='title'>πŸš€ AI Model Playground</h1>")
87
- gr.HTML("<p id='description'>Powered by Fireworks AI - Fine-tuned Model</p>")
88
 
89
  with gr.Row():
90
  with gr.Column(scale=3):
91
  chatbot = gr.Chatbot(
92
- height=500,
93
  bubble_full_width=False,
94
- avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=ai"),
95
- show_copy_button=True
 
 
 
 
96
  )
97
 
98
  with gr.Row():
99
  msg = gr.Textbox(
100
- placeholder="Type your message here...",
101
  show_label=False,
102
  scale=4,
103
- container=False
 
104
  )
105
- submit_btn = gr.Button("Send πŸ“€", scale=1, variant="primary")
106
 
107
  with gr.Row():
108
- clear_btn = gr.ClearButton([msg, chatbot], value="Clear Chat πŸ—‘οΈ")
 
109
 
110
  with gr.Column(scale=1):
111
- gr.Markdown("### βš™οΈ Model Parameters")
112
 
113
  temperature = gr.Slider(
114
  minimum=0,
115
  maximum=2,
116
  value=0.6,
117
  step=0.1,
118
- label="Temperature",
119
- info="Controls randomness. Lower = more focused"
120
  )
121
 
122
  max_tokens = gr.Slider(
@@ -124,8 +156,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
124
  maximum=4000,
125
  value=2000,
126
  step=100,
127
- label="Max Tokens",
128
- info="Maximum length of response"
129
  )
130
 
131
  top_p = gr.Slider(
@@ -133,8 +165,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
133
  maximum=1,
134
  value=1,
135
  step=0.05,
136
- label="Top P",
137
- info="Nucleus sampling threshold"
138
  )
139
 
140
  top_k = gr.Slider(
@@ -142,46 +174,108 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
142
  maximum=100,
143
  value=40,
144
  step=1,
145
- label="Top K",
146
- info="Number of top tokens to consider"
147
  )
148
 
149
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
- # Handle message submission
152
  def respond(message, chat_history, temp, max_tok, top_p_val, top_k_val):
 
 
 
153
  if not API_KEY:
154
- bot_message = "⚠️ Please set FIREWORKS_API_KEY in Hugging Face Spaces secrets!"
155
- else:
156
- bot_message = chat_with_model(message, chat_history, temp, max_tok, top_p_val, top_k_val)
157
 
158
- chat_history.append((message, bot_message))
159
- return "", chat_history
 
 
 
160
 
161
  msg.submit(
162
  respond,
163
  [msg, chatbot, temperature, max_tokens, top_p, top_k],
164
- [msg, chatbot]
165
  )
166
 
167
  submit_btn.click(
168
  respond,
169
  [msg, chatbot, temperature, max_tokens, top_p, top_k],
170
- [msg, chatbot]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  )
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  # Add examples
174
  gr.Examples(
175
  examples=[
176
- ["Hello! Can you introduce yourself?"],
177
- ["What can you help me with?"],
178
- ["Tell me an interesting fact about AI."],
 
179
  ],
180
  inputs=msg,
181
- label="πŸ’‘ Try these examples"
182
  )
183
 
184
-
 
 
 
 
 
185
 
186
  # Launch the app
187
  if __name__ == "__main__":
 
5
 
6
  # These will be set as Hugging Face Spaces secrets
7
  API_KEY = os.environ.get("FIREWORKS_API_KEY", "")
8
+ SYSTEM_PROMPT = os.environ.get("SYSTEM_PROMPT")
9
 
10
  # API endpoint
11
  API_URL = "https://api.fireworks.ai/inference/v1/chat/completions"
12
 
13
  def chat_with_model(message, history, temperature, max_tokens, top_p, top_k):
14
  """
15
+ Stream responses from the API
16
  """
17
  # Build conversation history
18
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
27
 
28
  # Prepare the request
29
  headers = {
30
+ "Accept": "text/event-stream",
31
  "Content-Type": "application/json",
32
  "Authorization": f"Bearer {API_KEY}"
33
  }
 
40
  "presence_penalty": 0,
41
  "frequency_penalty": 0,
42
  "temperature": temperature,
43
+ "messages": messages,
44
+ "stream": True
45
  }
46
 
47
  try:
48
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=120, stream=True)
49
  response.raise_for_status()
50
 
51
+ assistant_message = ""
52
+
53
+ for line in response.iter_lines():
54
+ if line:
55
+ line = line.decode('utf-8')
56
+ if line.startswith('data: '):
57
+ data = line[6:]
58
+ if data == '[DONE]':
59
+ break
60
+ try:
61
+ json_data = json.loads(data)
62
+ if 'choices' in json_data and len(json_data['choices']) > 0:
63
+ delta = json_data['choices'][0].get('delta', {})
64
+ content = delta.get('content', '')
65
+ if content:
66
+ assistant_message += content
67
+ yield assistant_message
68
+ except json.JSONDecodeError:
69
+ continue
70
+
71
+ if not assistant_message:
72
+ yield "❌ No response received from the model."
73
 
74
  except requests.exceptions.RequestException as e:
75
+ yield f"❌ Error: {str(e)}\n\nPlease check your API key in Hugging Face Spaces secrets."
76
+ except Exception as e:
77
+ yield f"❌ Unexpected error: {str(e)}"
78
 
79
  # Custom CSS for a modern look
80
  custom_css = """
81
  .gradio-container {
82
  font-family: 'Inter', sans-serif;
83
+ max-width: 1400px !important;
84
  }
85
  #title {
86
  text-align: center;
87
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
88
  -webkit-background-clip: text;
89
  -webkit-text-fill-color: transparent;
90
+ font-size: 3em;
91
+ font-weight: 800;
92
+ margin-bottom: 0.3em;
93
+ letter-spacing: -0.02em;
94
  }
95
+ #subtitle {
96
  text-align: center;
97
+ font-size: 1.2em;
98
+ color: #888;
99
  margin-bottom: 2em;
100
+ font-weight: 300;
101
  }
102
  .message-wrap {
103
+ border-radius: 16px !important;
104
+ }
105
+ footer {
106
+ display: none !important;
107
  }
108
  """
109
 
110
  # Create Gradio interface
111
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="purple", secondary_hue="pink")) as demo:
112
+ gr.HTML("<h1 id='title'>Palmyra-Sec Playground</h1>")
113
+ gr.HTML("<p id='subtitle'>Intelligent conversations powered by advanced language models</p>")
114
 
115
  with gr.Row():
116
  with gr.Column(scale=3):
117
  chatbot = gr.Chatbot(
118
+ height=550,
119
  bubble_full_width=False,
120
+ avatar_images=(
121
+ "https://api.dicebear.com/7.x/avataaars/svg?seed=user",
122
+ "https://api.dicebear.com/7.x/bottts-neutral/svg?seed=ai&backgroundColor=b6e3f4"
123
+ ),
124
+ show_copy_button=True,
125
+ likeable=True
126
  )
127
 
128
  with gr.Row():
129
  msg = gr.Textbox(
130
+ placeholder="Ask me anything...",
131
  show_label=False,
132
  scale=4,
133
+ container=False,
134
+ lines=2
135
  )
136
+ submit_btn = gr.Button("Send πŸ’¬", scale=1, variant="primary", size="lg")
137
 
138
  with gr.Row():
139
+ clear_btn = gr.ClearButton([msg, chatbot], value="πŸ—‘οΈ Clear", size="sm")
140
+ retry_btn = gr.Button("πŸ”„ Retry", size="sm", variant="secondary")
141
 
142
  with gr.Column(scale=1):
143
+ gr.Markdown("### βš™οΈ Model Settings")
144
 
145
  temperature = gr.Slider(
146
  minimum=0,
147
  maximum=2,
148
  value=0.6,
149
  step=0.1,
150
+ label="🌑️ Temperature",
151
+ info="Creativity level"
152
  )
153
 
154
  max_tokens = gr.Slider(
 
156
  maximum=4000,
157
  value=2000,
158
  step=100,
159
+ label="πŸ“ Max Tokens",
160
+ info="Response length"
161
  )
162
 
163
  top_p = gr.Slider(
 
165
  maximum=1,
166
  value=1,
167
  step=0.05,
168
+ label="🎯 Top P",
169
+ info="Diversity control"
170
  )
171
 
172
  top_k = gr.Slider(
 
174
  maximum=100,
175
  value=40,
176
  step=1,
177
+ label="πŸ”’ Top K",
178
+ info="Token selection"
179
  )
180
 
181
+ with gr.Accordion("πŸ“š Quick Presets", open=False):
182
+ gr.Markdown("""
183
+ **Creative** β†’ Temp: 0.9, Top P: 0.95
184
+ **Balanced** β†’ Temp: 0.6, Top P: 1.0
185
+ **Precise** β†’ Temp: 0.3, Top P: 0.9
186
+ """)
187
+
188
+ with gr.Row():
189
+ creative_btn = gr.Button("🎨 Creative", size="sm")
190
+ balanced_btn = gr.Button("βš–οΈ Balanced", size="sm")
191
+ precise_btn = gr.Button("🎯 Precise", size="sm")
192
+
193
+ gr.Markdown("---")
194
+ gr.Markdown("### πŸ”§ Setup Guide")
195
+ gr.Markdown("""
196
+ **Space Settings β†’ Secrets:**
197
+ - `FIREWORKS_API_KEY`
198
+ - `SYSTEM_PROMPT`
199
+
200
+ Then restart your Space ♻️
201
+ """)
202
 
203
+ # Handle message submission with streaming
204
  def respond(message, chat_history, temp, max_tok, top_p_val, top_k_val):
205
+ if not message.strip():
206
+ return chat_history, ""
207
+
208
  if not API_KEY:
209
+ chat_history.append((message, "⚠️ Please configure API key in Space secrets!"))
210
+ return chat_history, ""
 
211
 
212
+ chat_history.append((message, ""))
213
+
214
+ for partial_response in chat_with_model(message, chat_history[:-1], temp, max_tok, top_p_val, top_k_val):
215
+ chat_history[-1] = (message, partial_response)
216
+ yield chat_history, ""
217
 
218
  msg.submit(
219
  respond,
220
  [msg, chatbot, temperature, max_tokens, top_p, top_k],
221
+ [chatbot, msg]
222
  )
223
 
224
  submit_btn.click(
225
  respond,
226
  [msg, chatbot, temperature, max_tokens, top_p, top_k],
227
+ [chatbot, msg]
228
+ )
229
+
230
+ # Retry last message
231
+ def retry_last(chat_history, temp, max_tok, top_p_val, top_k_val):
232
+ if not chat_history:
233
+ return chat_history
234
+
235
+ last_message = chat_history[-1][0]
236
+ chat_history = chat_history[:-1]
237
+
238
+ for updated_history, _ in respond(last_message, chat_history, temp, max_tok, top_p_val, top_k_val):
239
+ yield updated_history
240
+
241
+ retry_btn.click(
242
+ retry_last,
243
+ [chatbot, temperature, max_tokens, top_p, top_k],
244
+ [chatbot]
245
  )
246
 
247
+ # Preset buttons
248
+ def set_creative():
249
+ return 0.9, 0.95
250
+
251
+ def set_balanced():
252
+ return 0.6, 1.0
253
+
254
+ def set_precise():
255
+ return 0.3, 0.9
256
+
257
+ creative_btn.click(set_creative, None, [temperature, top_p])
258
+ balanced_btn.click(set_balanced, None, [temperature, top_p])
259
+ precise_btn.click(set_precise, None, [temperature, top_p])
260
+
261
  # Add examples
262
  gr.Examples(
263
  examples=[
264
+ ["Write a creative short story about time travel"],
265
+ ["Explain quantum computing in simple terms"],
266
+ ["Help me brainstorm ideas for a mobile app"],
267
+ ["What are the best practices for Python code?"],
268
  ],
269
  inputs=msg,
270
+ label="πŸ’‘ Example Prompts"
271
  )
272
 
273
+ gr.Markdown("""
274
+ ---
275
+ <div style='text-align: center; color: #888; font-size: 0.9em;'>
276
+ πŸ”’ <b>Privacy First</b> β€’ All credentials stored securely β€’ Conversations are ephemeral
277
+ </div>
278
+ """)
279
 
280
  # Launch the app
281
  if __name__ == "__main__":