Spaces:
Runtime error
Runtime error
Muhirwa12a
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Define API parameters
|
6 |
+
API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-mamba-7b"
|
7 |
+
API_KEY = os.getenv("HUGGINGFACE_TOKEN")
|
8 |
+
|
9 |
+
# Ensure the token is available
|
10 |
+
if not API_KEY:
|
11 |
+
raise ValueError("Hugging Face API token not found. Please set HUGGINGFACE_TOKEN environment variable.")
|
12 |
+
|
13 |
+
# Set up headers for Hugging Face API authentication
|
14 |
+
headers = {
|
15 |
+
"Authorization": f"Bearer {API_KEY}"
|
16 |
+
}
|
17 |
+
|
18 |
+
# Function to query the model
|
19 |
+
def query_model(user_input):
|
20 |
+
payload = {
|
21 |
+
"inputs": user_input,
|
22 |
+
"parameters": {
|
23 |
+
"temperature": 0.7,
|
24 |
+
"max_length": 150
|
25 |
+
}
|
26 |
+
}
|
27 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
28 |
+
if response.status_code == 200:
|
29 |
+
return response.json()[0]['generated_text']
|
30 |
+
else:
|
31 |
+
return f"Error {response.status_code}: {response.text}"
|
32 |
+
|
33 |
+
# Chatbot function that manages conversation history
|
34 |
+
def chatbot(input_text, history=[]):
|
35 |
+
if input_text.lower() in ["exit", "quit"]:
|
36 |
+
return "Take care! Remember, seeking support is a strength.", history
|
37 |
+
|
38 |
+
# Append the user's message to the history
|
39 |
+
history.append(("You", input_text))
|
40 |
+
|
41 |
+
# Get the model's response
|
42 |
+
response = query_model(input_text)
|
43 |
+
|
44 |
+
# Append the model's response to the history
|
45 |
+
history.append(("Bot", response))
|
46 |
+
|
47 |
+
# Return the response and updated history for the UI
|
48 |
+
return response, history
|
49 |
+
|
50 |
+
# Gradio UI Layout
|
51 |
+
with gr.Blocks() as demo:
|
52 |
+
gr.Markdown(
|
53 |
+
"""
|
54 |
+
# 🧘♀️ Mental Health Chatbot
|
55 |
+
### Hi! I'm here to listen and provide support. How can I help you today?
|
56 |
+
"""
|
57 |
+
)
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
chatbot_output = gr.Chatbot(label="Chatbot", value=[])
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
|
64 |
+
send_button = gr.Button("Send")
|
65 |
+
|
66 |
+
# Update chatbot output when the user submits a message
|
67 |
+
def respond(user_input, history):
|
68 |
+
response, history = chatbot(user_input, history)
|
69 |
+
return history, gr.update(value="")
|
70 |
+
|
71 |
+
# Clear the input box after sending the message
|
72 |
+
send_button.click(respond, inputs=[user_input, chatbot_output], outputs=[chatbot_output, user_input])
|
73 |
+
|
74 |
+
# Launch the Gradio app
|
75 |
+
demo.launch()
|