Spaces:
Running
Running
import gradio as gr | |
import openai | |
import os | |
# Set OpenAI API Key | |
openai.api_key = "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" | |
openai.api_base = "https://api.groq.com/openai/v1" | |
# Function to get response from OpenAI API | |
def get_groq_response(message, category): | |
# Define system message based on category | |
system_messages = { | |
"Stress Management": "Provide soothing advice and tips to help the user manage stress. Be calm, empathetic, and reassuring.", | |
"Career Advice": "Offer professional and constructive career advice. Be encouraging, insightful, and action-oriented.", | |
"General": "Engage in general conversation. Be friendly, approachable, and easygoing.", | |
"Data Analysis": """ | |
You are an advanced AI tool designed to assist users in understanding and drawing insights from their data. Your objective is to analyze the data provided, identify key patterns, trends, and anomalies, and present the information in a clear, structured, and insightful manner. You must: | |
1. Data Analysis: Thoroughly analyze the data and identify key insights such as trends, correlations, patterns, and outliers. | |
2. Clear Explanation: Provide detailed explanations of the data, breaking it down into simple and understandable segments. Use analogies or examples if necessary to simplify complex concepts. | |
3. Visual Representation: Where applicable, create tables, graphs, pie charts, or histograms to present the data visually, enhancing comprehension. | |
4. Actionable Insights: Offer actionable recommendations or key takeaways from the data, making it easy for users to derive value from your analysis. | |
5. Contextual Understanding: Ensure you understand the context of the data provided. If the data is part of a larger problem or scenario, clarify how it fits into the bigger picture. | |
6. Customization: Adapt your analysis to the user's specific needs, whether itโs business, personal, or scientific data. | |
""", | |
# Add more categories as needed... | |
} | |
system_message = system_messages.get(category, "Respond appropriately to the user's input.") | |
try: | |
response = openai.ChatCompletion.create( | |
model="llama-3.3-70b-versatile", | |
messages=[ | |
{"role": "system", "content": system_message}, | |
{"role": "user", "content": message} | |
] | |
) | |
return response.choices[0].message["content"] | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Chatbot function | |
def chatbot(user_input, category, history=[]): | |
bot_response = get_groq_response(user_input, category) | |
history.append((user_input, bot_response)) | |
return history, history | |
# Categories | |
categories = { | |
"Academic Support": ["Study Tips", "Exam Preparation", "Project Guidance", "Time Management"], | |
"Mental & Physical Wellness": ["Stress Management", "Motivation & Focus", "Mental Health", "Physical Health"], | |
"Career & Financial": ["Networking & Career Building", "Resume Building", "Interview Preparation"], | |
"Data Analysis": ["Analyze Business Data", "Analyze Scientific Data", "Analyze Personal Data"] # New category for Data Analysis | |
} | |
# Gradio Interface | |
with gr.Blocks() as chat_interface: | |
with gr.Row(): | |
gr.Markdown("<h1>Encourage AI ๐</h1>") | |
with gr.Row(): | |
main_category = gr.Radio( | |
label="Main Category", | |
choices=list(categories.keys()), | |
value="Academic Support" | |
) | |
sub_category = gr.Dropdown( | |
label="Subcategory", | |
choices=categories["Academic Support"], | |
value="Study Tips" | |
) | |
def update_subcategories(selected_main_category): | |
new_subcategories = categories.get(selected_main_category, []) | |
return gr.update(choices=new_subcategories, value=new_subcategories[0] if new_subcategories else None) | |
main_category.change(update_subcategories, inputs=main_category, outputs=sub_category) | |
with gr.Row(): | |
user_input = gr.Textbox(label="Your Message", placeholder="Type something...") | |
send_button = gr.Button("Send") | |
with gr.Row(): | |
chatbot_output = gr.Chatbot(label="Chat History") | |
def handle_chat(user_input, sub_category, history): | |
if not user_input.strip(): | |
return history, history | |
updated_history, _ = chatbot(user_input, sub_category, history) | |
return updated_history, updated_history | |
send_button.click( | |
handle_chat, | |
inputs=[user_input, sub_category, chatbot_output], | |
outputs=[chatbot_output, chatbot_output] | |
) | |
# Additional field for Data Analysis inputs (only visible when Data Analysis is selected) | |
with gr.Row(visible=False) as data_analysis_row: | |
data_input = gr.Textbox(label="Input Your Data", placeholder="Paste or type your data here...") | |
analyze_button = gr.Button("Analyze Data") | |
data_analysis_output = gr.Textbox(label="Analysis Result", interactive=False) | |
def toggle_data_analysis_visibility(category): | |
if category == "Data Analysis": | |
return gr.update(visible=True) | |
else: | |
return gr.update(visible=False) | |
main_category.change(toggle_data_analysis_visibility, inputs=main_category, outputs=data_analysis_row) | |
# Handle Data Analysis | |
def handle_data_analysis(data_input): | |
if not data_input.strip(): | |
return "Please provide valid data for analysis." | |
analysis_result = get_groq_response(data_input, "Data Analysis") | |
return analysis_result | |
analyze_button.click( | |
handle_data_analysis, | |
inputs=data_input, | |
outputs=data_analysis_output | |
) | |
chat_interface.launch() |