Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Advanced User Interface
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import openai
|
| 4 |
+
|
| 5 |
+
# Set OpenAI API Key
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
| 8 |
+
|
| 9 |
+
# Dictionary to store categorized chats
|
| 10 |
+
saved_chats = {
|
| 11 |
+
"Stress Management": [],
|
| 12 |
+
"Career Advice": [],
|
| 13 |
+
"General": []
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Function to get response from GROQ API
|
| 17 |
+
def get_groq_response(message):
|
| 18 |
+
try:
|
| 19 |
+
response = openai.ChatCompletion.create(
|
| 20 |
+
model="llama-3.1-70b-versatile",
|
| 21 |
+
messages=[
|
| 22 |
+
{"role": "user", "content": message},
|
| 23 |
+
{"role": "system", "content": "You will talk like a Motivational Speaker to help people come out of stress."}
|
| 24 |
+
]
|
| 25 |
+
)
|
| 26 |
+
return response.choices[0].message["content"]
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
# Function to classify messages based on the topic
|
| 31 |
+
def classify_message(user_message, bot_response):
|
| 32 |
+
if "stress" in user_message.lower():
|
| 33 |
+
saved_chats["Stress Management"].append((user_message, bot_response))
|
| 34 |
+
return "Stress Management"
|
| 35 |
+
elif "career" in user_message.lower():
|
| 36 |
+
saved_chats["Career Advice"].append((user_message, bot_response))
|
| 37 |
+
return "Career Advice"
|
| 38 |
+
else:
|
| 39 |
+
saved_chats["General"].append((user_message, bot_response))
|
| 40 |
+
return "General"
|
| 41 |
+
|
| 42 |
+
# Chatbot function
|
| 43 |
+
def chatbot(user_input, history=[]):
|
| 44 |
+
bot_response = get_groq_response(user_input) # Get bot response
|
| 45 |
+
topic = classify_message(user_input, bot_response) # Classify the message
|
| 46 |
+
history.append((f"({topic}) You: {user_input}", f"Motivator Bot: {bot_response}")) # Update conversation history
|
| 47 |
+
return history, saved_chats # Return history and saved chats
|
| 48 |
+
|
| 49 |
+
# Gradio Interface setup
|
| 50 |
+
def display_saved_chats():
|
| 51 |
+
# Format saved chats for display
|
| 52 |
+
display = ""
|
| 53 |
+
for category, chats in saved_chats.items():
|
| 54 |
+
display += f"### {category} Chats\n"
|
| 55 |
+
for user_message, bot_response in chats:
|
| 56 |
+
display += f"- **You**: {user_message}\n - **Motivator Bot**: {bot_response}\n\n"
|
| 57 |
+
return display.strip()
|
| 58 |
+
|
| 59 |
+
# Customized Gradio Interface
|
| 60 |
+
chat_interface = gr.Blocks(theme="dark")
|
| 61 |
+
|
| 62 |
+
with chat_interface:
|
| 63 |
+
gr.Markdown("## 🚀 **Motivational Chatbot**\n\n**Feeling Stressed? Let's Talk!**\n\nThis chatbot is designed to help you relieve stress and find motivation. Ask anything, share your worries, or simply seek some words of encouragement!")
|
| 64 |
+
chatbot_output = gr.Chatbot(label="Motivator Bot")
|
| 65 |
+
with gr.Row():
|
| 66 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type your message...")
|
| 67 |
+
send_button = gr.Button("Send")
|
| 68 |
+
saved_chats_display = gr.Markdown(label="Saved Chats")
|
| 69 |
+
refresh_button = gr.Button("Refresh Saved Chats")
|
| 70 |
+
|
| 71 |
+
# Chat interaction
|
| 72 |
+
def handle_interaction(user_input, history):
|
| 73 |
+
if not user_input.strip():
|
| 74 |
+
return history, display_saved_chats()
|
| 75 |
+
updated_history, _ = chatbot(user_input, history)
|
| 76 |
+
return updated_history, display_saved_chats()
|
| 77 |
+
|
| 78 |
+
send_button.click(handle_interaction, inputs=[user_input, chatbot_output], outputs=[chatbot_output, saved_chats_display])
|
| 79 |
+
refresh_button.click(display_saved_chats, inputs=[], outputs=saved_chats_display)
|
| 80 |
+
|
| 81 |
+
# Launch the Interface
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
chat_interface.launch(debug=True)
|