RahulGanapathy commited on
Commit
0fb0876
·
verified ·
1 Parent(s): f055837

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
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
- # FastAPI app for Telegram bot
8
- app = FastAPI()
9
 
10
- # Hugging Face Model Client
11
- client = InferenceClient("RahulGanapathy/MisInfo-ChatBot")
 
 
 
12
 
13
- # Define request schema for API
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
- Handles chat interaction in Gradio UI.
35
- """
36
- messages = [{}"role":"system","content":"You are a helpful Assistant. You let the user to know if the information they share with you is true or false. You return the actual facts behind the information given, after stating the state of Truth or False.",{"role": "user", "content": message}]
37
- response = client.text_generation(prompt=message, max_new_tokens=100)
38
- return response
39
-
40
- # Gradio UI Setup
41
- demo = gr.ChatInterface(respond)
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()