import gradio as gr import openai import os # Set OpenAI API Key openai.api_key = os.getenv("TRY_NEW_THINGS") 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.", # 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.1-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"], } # Gradio Interface with gr.Blocks() as chat_interface: with gr.Row(): gr.Markdown("

Encourage AI 🌟

") 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] ) chat_interface.launch()