Bhaskar2611 commited on
Commit
789f6c7
·
verified ·
1 Parent(s): c4b2e34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -6
app.py CHANGED
@@ -164,11 +164,70 @@ For more information on `huggingface_hub` Inference API support, please check th
164
  # if __name__ == "__main__":
165
  # demo.launch()
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  import gradio as gr
168
  from huggingface_hub import InferenceClient
 
 
169
 
170
- # 1. Instantiate with named model param
171
- client = InferenceClient(model="Qwen/Qwen2.5-Coder-32B-Instruct")
 
 
 
 
 
 
 
 
 
 
 
172
 
173
  def respond(message, history: list[tuple[str, str]]):
174
  system_message = (
@@ -181,7 +240,7 @@ def respond(message, history: list[tuple[str, str]]):
181
  temperature = 0.7
182
  top_p = 0.95
183
 
184
- # Build messages in OpenAI-compatible format
185
  messages = [{"role": "system", "content": system_message}]
186
  for user_msg, assistant_msg in history:
187
  if user_msg:
@@ -191,7 +250,7 @@ def respond(message, history: list[tuple[str, str]]):
191
  messages.append({"role": "user", "content": message})
192
 
193
  response = ""
194
- # 2. Use named parameters and alias if desired
195
  for chunk in client.chat.completions.create(
196
  model="Qwen/Qwen2.5-Coder-32B-Instruct",
197
  messages=messages,
@@ -200,12 +259,11 @@ def respond(message, history: list[tuple[str, str]]):
200
  temperature=temperature,
201
  top_p=top_p,
202
  ):
203
- # 3. Extract token content
204
  token = chunk.choices[0].delta.content or ""
205
  response += token
206
  yield response
207
 
208
- # 4. Wire up Gradio chat interface
209
  demo = gr.ChatInterface(respond, type="messages")
210
 
211
  if __name__ == "__main__":
@@ -215,3 +273,4 @@ if __name__ == "__main__":
215
 
216
 
217
 
 
 
164
  # if __name__ == "__main__":
165
  # demo.launch()
166
 
167
+ # import gradio as gr
168
+ # from huggingface_hub import InferenceClient
169
+
170
+ # # 1. Instantiate with named model param
171
+ # client = InferenceClient(model="Qwen/Qwen2.5-Coder-32B-Instruct")
172
+
173
+ # def respond(message, history: list[tuple[str, str]]):
174
+ # system_message = (
175
+ # "You are a helpful and experienced coding assistant specialized in web development. "
176
+ # "Help the user by generating complete and functional code for building websites. "
177
+ # "You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.) "
178
+ # "based on their requirements."
179
+ # )
180
+ # max_tokens = 2048
181
+ # temperature = 0.7
182
+ # top_p = 0.95
183
+
184
+ # # Build messages in OpenAI-compatible format
185
+ # messages = [{"role": "system", "content": system_message}]
186
+ # for user_msg, assistant_msg in history:
187
+ # if user_msg:
188
+ # messages.append({"role": "user", "content": user_msg})
189
+ # if assistant_msg:
190
+ # messages.append({"role": "assistant", "content": assistant_msg})
191
+ # messages.append({"role": "user", "content": message})
192
+
193
+ # response = ""
194
+ # # 2. Use named parameters and alias if desired
195
+ # for chunk in client.chat.completions.create(
196
+ # model="Qwen/Qwen2.5-Coder-32B-Instruct",
197
+ # messages=messages,
198
+ # max_tokens=max_tokens,
199
+ # stream=True,
200
+ # temperature=temperature,
201
+ # top_p=top_p,
202
+ # ):
203
+ # # 3. Extract token content
204
+ # token = chunk.choices[0].delta.content or ""
205
+ # response += token
206
+ # yield response
207
+
208
+ # # 4. Wire up Gradio chat interface
209
+ # demo = gr.ChatInterface(respond, type="messages")
210
+
211
+ # if __name__ == "__main__":
212
+ # demo.launch()
213
  import gradio as gr
214
  from huggingface_hub import InferenceClient
215
+ from dotenv import load_dotenv
216
+ import os
217
 
218
+ # Load environment variables from .env file
219
+ load_dotenv()
220
+ hf_token = os.getenv("HF_TOKEN")
221
+
222
+ # Ensure token is available
223
+ if hf_token is None:
224
+ raise ValueError("HUGGINGFACEHUB_API_TOKEN is not set in .env file or environment.")
225
+
226
+ # Instantiate Hugging Face Inference Client with token
227
+ client = InferenceClient(
228
+ model="Qwen/Qwen2.5-Coder-32B-Instruct",
229
+ token=hf_token
230
+ )
231
 
232
  def respond(message, history: list[tuple[str, str]]):
233
  system_message = (
 
240
  temperature = 0.7
241
  top_p = 0.95
242
 
243
+ # Build conversation history
244
  messages = [{"role": "system", "content": system_message}]
245
  for user_msg, assistant_msg in history:
246
  if user_msg:
 
250
  messages.append({"role": "user", "content": message})
251
 
252
  response = ""
253
+ # Stream the response from the model
254
  for chunk in client.chat.completions.create(
255
  model="Qwen/Qwen2.5-Coder-32B-Instruct",
256
  messages=messages,
 
259
  temperature=temperature,
260
  top_p=top_p,
261
  ):
 
262
  token = chunk.choices[0].delta.content or ""
263
  response += token
264
  yield response
265
 
266
+ # Gradio UI
267
  demo = gr.ChatInterface(respond, type="messages")
268
 
269
  if __name__ == "__main__":
 
273
 
274
 
275
 
276
+