RahulGanapathy commited on
Commit
85b47e4
·
verified ·
1 Parent(s): 19b1aad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -2,8 +2,8 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # 🔑 Load Hugging Face API Token from environment variable
6
- HF_API_TOKEN = os.getenv("HF_API_TOKEN") # Ensure this is set
7
 
8
  # Initialize Hugging Face model client with authentication
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_API_TOKEN)
@@ -16,9 +16,13 @@ SYSTEM_MESSAGE = (
16
 
17
  # Function to handle chatbot responses
18
  def respond(message, history):
19
- # Convert history into OpenAI-style messages
 
 
 
20
  messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
21
 
 
22
  for user, bot in history:
23
  messages.append({"role": "user", "content": user})
24
  messages.append({"role": "assistant", "content": bot})
@@ -27,9 +31,13 @@ def respond(message, history):
27
 
28
  # Get response from Hugging Face model
29
  response = client.chat_completion(messages, max_tokens=200, temperature=0.7)
30
- bot_reply = response.choices[0].message.content
31
 
32
- history.append((message, bot_reply)) # Ensure return matches Gradio format
 
 
 
 
 
33
  return history, bot_reply
34
 
35
  # Create Gradio chatbot UI
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # 🔑 Load Hugging Face API Token
6
+ HF_API_TOKEN = os.getenv("HF_API_TOKEN")
7
 
8
  # Initialize Hugging Face model client with authentication
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_API_TOKEN)
 
16
 
17
  # Function to handle chatbot responses
18
  def respond(message, history):
19
+ # Ensure history is a list of tuples
20
+ if not isinstance(history, list):
21
+ history = []
22
+
23
  messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
24
 
25
+ # Ensure history is correctly formatted
26
  for user, bot in history:
27
  messages.append({"role": "user", "content": user})
28
  messages.append({"role": "assistant", "content": bot})
 
31
 
32
  # Get response from Hugging Face model
33
  response = client.chat_completion(messages, max_tokens=200, temperature=0.7)
 
34
 
35
+ # Extract text from API response
36
+ bot_reply = response.choices[0].message.content.strip()
37
+
38
+ # ✅ Append correctly formatted tuple to history
39
+ history.append((message, bot_reply))
40
+
41
  return history, bot_reply
42
 
43
  # Create Gradio chatbot UI