File size: 1,793 Bytes
e95d517
f055837
19b1aad
5542c67
85b47e4
 
19b1aad
 
 
0f840ba
0fb0876
 
 
 
 
f49da4b
0fb0876
f055837
8fbc968
85b47e4
 
4a06593
 
 
 
 
 
0fb0876
 
5a89ad3
 
5265021
5a89ad3
85b47e4
 
 
 
 
5a89ad3
c53e8fd
0fb0876
 
42399aa
 
8fbc968
c53e8fd
0fb0876
 
8fbc968
0fb0876
 
5c297e4
0fb0876
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
from huggingface_hub import InferenceClient
import os

# πŸ”‘ Load Hugging Face API Token
HF_API_TOKEN = os.getenv("HF_API_TOKEN")

# Initialize Hugging Face model client with authentication
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_API_TOKEN)

# System message setting the chatbot's behavior
SYSTEM_MESSAGE = (
    "You are a helpful Assistant. You let the user 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."
)

# Function to handle chatbot responses
def respond(message, history):
    if history is None:
        history = []

    messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
    
    for user, bot in history:
        messages.append({"role": "user", "content": user})
        messages.append({"role": "assistant", "content": bot})

    messages.append({"role": "user", "content": message})

    # βœ… Ensure streaming is disabled to prevent looping
    response = client.chat_completion(messages, max_tokens=200, temperature=0.7, stream=False)

    # βœ… Extract response only once
    bot_reply = response.choices[0].message.content.strip()

    # βœ… Append correctly formatted tuple to history
    history.append((message, bot_reply))

    # βœ… Return chatbot history and state
    return history, history

# Create Gradio chatbot UI
demo = gr.Interface(
    fn=respond,
    inputs=["text", "state"],  # βœ… Pass state properly
    outputs=["chatbot", "state"],  # βœ… Ensure state is included in outputs
    title="Misinformation Detection Chatbot",
    description="Ask anything, and the chatbot will verify whether it's true or false.",
    live=True,
)

if __name__ == "__main__":
    demo.launch()