Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +75 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load GROQ API key from environment
|
| 7 |
+
load_dotenv()
|
| 8 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 9 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 10 |
+
MODEL_NAME = "llama3-8b-8192"
|
| 11 |
+
|
| 12 |
+
SYSTEM_PROMPT = """
|
| 13 |
+
You are an intelligent and helpful machine learning assistant.
|
| 14 |
+
Your task is to help users choose appropriate machine learning models based on their problem type
|
| 15 |
+
(classification, regression, clustering, etc.), dataset characteristics, accuracy needs, and resource constraints.
|
| 16 |
+
Explain recommendations clearly and concisely, and offer guidance on why a model is suitable.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
def query_groq(message, chat_history):
|
| 20 |
+
headers = {
|
| 21 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 22 |
+
"Content-Type": "application/json"
|
| 23 |
+
}
|
| 24 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 25 |
+
for user, bot in chat_history:
|
| 26 |
+
messages.append({"role": "user", "content": user})
|
| 27 |
+
messages.append({"role": "assistant", "content": bot})
|
| 28 |
+
messages.append({"role": "user", "content": message})
|
| 29 |
+
|
| 30 |
+
response = requests.post(GROQ_API_URL, headers=headers, json={
|
| 31 |
+
"model": MODEL_NAME,
|
| 32 |
+
"messages": messages,
|
| 33 |
+
"temperature": 0.7
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
| 38 |
+
return reply
|
| 39 |
+
else:
|
| 40 |
+
return f"Error {response.status_code}: {response.text}"
|
| 41 |
+
|
| 42 |
+
def generate_question(problem_type, notes):
|
| 43 |
+
return f"I am working on a {problem_type} problem. {notes} What machine learning model would be suitable?"
|
| 44 |
+
|
| 45 |
+
def respond(message, chat_history):
|
| 46 |
+
bot_reply = query_groq(message, chat_history)
|
| 47 |
+
chat_history.append((message, bot_reply))
|
| 48 |
+
return "", chat_history
|
| 49 |
+
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("## 🤖 ML Model Selector Chatbot (Powered by GROQ LLM)")
|
| 52 |
+
gr.Markdown("Use the dropdown to specify your ML problem and get recommendations!")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
problem_type = gr.Dropdown(
|
| 56 |
+
choices=["Classification", "Regression", "Clustering", "Anomaly Detection", "Dimensionality Reduction"],
|
| 57 |
+
label="Select your ML Problem Type",
|
| 58 |
+
value="Classification"
|
| 59 |
+
)
|
| 60 |
+
notes = gr.Textbox(label="Add extra details (optional)")
|
| 61 |
+
|
| 62 |
+
generate_btn = gr.Button("Generate Question")
|
| 63 |
+
chatbot = gr.Chatbot()
|
| 64 |
+
msg = gr.Textbox(label="Ask your question or use generated one above")
|
| 65 |
+
clear = gr.Button("Clear Chat")
|
| 66 |
+
state = gr.State([])
|
| 67 |
+
|
| 68 |
+
def fill_generated_question(ptype, detail):
|
| 69 |
+
return generate_question(ptype, detail)
|
| 70 |
+
|
| 71 |
+
generate_btn.click(fill_generated_question, [problem_type, notes], msg)
|
| 72 |
+
msg.submit(respond, [msg, state], [msg, chatbot])
|
| 73 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 74 |
+
|
| 75 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|