Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# System message setting the chatbot's behavior
|
| 8 |
SYSTEM_MESSAGE = (
|
|
@@ -15,31 +19,26 @@ def respond(message, history):
|
|
| 15 |
# Convert history into OpenAI-style messages
|
| 16 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 17 |
|
| 18 |
-
# Convert Gradio history format (tuples) to OpenAI format
|
| 19 |
for user, bot in history:
|
| 20 |
messages.append({"role": "user", "content": user})
|
| 21 |
messages.append({"role": "assistant", "content": bot})
|
| 22 |
|
| 23 |
-
# Append latest user input
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
|
| 26 |
# Get response from Hugging Face model
|
| 27 |
response = client.chat_completion(messages, max_tokens=200, temperature=0.7)
|
| 28 |
bot_reply = response.choices[0].message.content
|
| 29 |
|
| 30 |
-
|
| 31 |
-
history
|
| 32 |
-
|
| 33 |
-
return history, bot_reply # ✅ Ensure return matches Gradio format
|
| 34 |
|
| 35 |
# Create Gradio chatbot UI
|
| 36 |
demo = gr.ChatInterface(
|
| 37 |
respond,
|
| 38 |
-
chatbot=gr.Chatbot(),
|
| 39 |
title="Misinformation Detection Chatbot",
|
| 40 |
description="Ask anything, and the chatbot will verify whether it's true or false.",
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Launch Gradio UI
|
| 44 |
if __name__ == "__main__":
|
| 45 |
demo.launch()
|
|
|
|
| 1 |
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)
|
| 10 |
|
| 11 |
# System message setting the chatbot's behavior
|
| 12 |
SYSTEM_MESSAGE = (
|
|
|
|
| 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})
|
| 25 |
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 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
|
| 36 |
demo = gr.ChatInterface(
|
| 37 |
respond,
|
| 38 |
+
chatbot=gr.Chatbot(),
|
| 39 |
title="Misinformation Detection Chatbot",
|
| 40 |
description="Ask anything, and the chatbot will verify whether it's true or false.",
|
| 41 |
)
|
| 42 |
|
|
|
|
| 43 |
if __name__ == "__main__":
|
| 44 |
demo.launch()
|