Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
class RequestData(BaseModel):
|
| 15 |
-
text: str
|
| 16 |
-
chat_id: int
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@app.post("/predict")
|
| 20 |
-
def predict(data: RequestData):
|
| 21 |
-
"""
|
| 22 |
-
API endpoint to process requests from the Telegram bot.
|
| 23 |
-
Takes user text input and returns model response.
|
| 24 |
-
"""
|
| 25 |
-
try:
|
| 26 |
-
response = client.text_generation(prompt=data.text, max_new_tokens=100)
|
| 27 |
-
return {"response": response}
|
| 28 |
-
except Exception as e:
|
| 29 |
-
return {"response": f"Error: {str(e)}"}
|
| 30 |
-
|
| 31 |
-
# Function for Gradio UI interaction
|
| 32 |
def respond(message, history):
|
| 33 |
-
"""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize Hugging Face model client
|
| 5 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# System message setting the chatbot's behavior
|
| 8 |
+
SYSTEM_MESSAGE = (
|
| 9 |
+
"You are a helpful Assistant. You let the user know if the information they share with you is true or false. "
|
| 10 |
+
"You return the actual facts behind the information given, after stating the state of Truth or False."
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
# Function to handle chatbot responses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def respond(message, history):
|
| 15 |
+
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 16 |
+
|
| 17 |
+
for user_msg, bot_reply in history:
|
| 18 |
+
if user_msg:
|
| 19 |
+
messages.append({"role": "user", "content": user_msg})
|
| 20 |
+
if bot_reply:
|
| 21 |
+
messages.append({"role": "assistant", "content": bot_reply})
|
| 22 |
+
|
| 23 |
+
messages.append({"role": "user", "content": message})
|
| 24 |
+
|
| 25 |
+
response = client.chat_completion(messages, max_tokens=200, temperature=0.7)
|
| 26 |
+
bot_reply = response.choices[0].message.content
|
| 27 |
+
return bot_reply
|
| 28 |
+
|
| 29 |
+
# Create Gradio chatbot UI
|
| 30 |
+
demo = gr.ChatInterface(
|
| 31 |
+
respond,
|
| 32 |
+
chatbot=gr.Chatbot(type="messages"),
|
| 33 |
+
title="Misinformation Detection Chatbot",
|
| 34 |
+
description="Ask anything, and the chatbot will verify whether it's true or false.",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Launch Gradio UI
|
| 38 |
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|