Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| import requests | |
| from dotenv import load_dotenv | |
| # Load GROQ API key from environment | |
| load_dotenv() | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
| GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" | |
| MODEL_NAME = "llama3-8b-8192" | |
| SYSTEM_PROMPT = """ | |
| You are an intelligent and helpful machine learning assistant. | |
| Your task is to help users choose appropriate machine learning models based on their problem type | |
| (classification, regression, clustering, etc.), dataset characteristics, accuracy needs, and resource constraints. | |
| Explain recommendations clearly and concisely, and offer guidance on why a model is suitable. | |
| """ | |
| def query_groq(message, chat_history): | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] | |
| for user, bot in chat_history: | |
| messages.append({"role": "user", "content": user}) | |
| messages.append({"role": "assistant", "content": bot}) | |
| messages.append({"role": "user", "content": message}) | |
| response = requests.post(GROQ_API_URL, headers=headers, json={ | |
| "model": MODEL_NAME, | |
| "messages": messages, | |
| "temperature": 0.7 | |
| }) | |
| if response.status_code == 200: | |
| reply = response.json()["choices"][0]["message"]["content"] | |
| return reply | |
| else: | |
| return f"Error {response.status_code}: {response.text}" | |
| def generate_question(problem_type, notes): | |
| return f"I am working on a {problem_type} problem. {notes} What machine learning model would be suitable?" | |
| def respond(message, chat_history): | |
| bot_reply = query_groq(message, chat_history) | |
| chat_history.append((message, bot_reply)) | |
| return "", chat_history | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🤖 ML Model Selector Chatbot (Powered by GROQ LLM)") | |
| gr.Markdown("Use the dropdown to specify your ML problem and get recommendations!") | |
| with gr.Row(): | |
| problem_type = gr.Dropdown( | |
| choices=["Classification", "Regression", "Clustering", "Anomaly Detection", "Dimensionality Reduction"], | |
| label="Select your ML Problem Type", | |
| value="Classification" | |
| ) | |
| notes = gr.Textbox(label="Add extra details (optional)") | |
| generate_btn = gr.Button("Generate Question") | |
| chatbot = gr.Chatbot() | |
| msg = gr.Textbox(label="Ask your question or use generated one above") | |
| clear = gr.Button("Clear Chat") | |
| state = gr.State([]) | |
| def fill_generated_question(ptype, detail): | |
| return generate_question(ptype, detail) | |
| generate_btn.click(fill_generated_question, [problem_type, notes], msg) | |
| msg.submit(respond, [msg, state], [msg, chatbot]) | |
| clear.click(lambda: ([], []), None, [chatbot, state]) | |
| demo.launch() |