Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,63 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
load_dotenv()
|
| 8 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
| 9 |
if not GROQ_API_KEY:
|
| 10 |
-
raise ValueError("Missing GROQ_API_KEY. Set it in
|
| 11 |
|
| 12 |
-
#
|
| 13 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 14 |
MODEL = "llama3-8b-8192"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"Authorization": f"Bearer {GROQ_API_KEY.strip()}",
|
| 18 |
"Content-Type": "application/json"
|
| 19 |
}
|
| 20 |
|
| 21 |
-
# Core
|
| 22 |
-
def
|
| 23 |
-
messages = [
|
| 24 |
-
{
|
| 25 |
-
"role": "system",
|
| 26 |
-
"content": (
|
| 27 |
-
"You are an expert AI tutor. Answer student questions clearly and in short, "
|
| 28 |
-
"bite-sized explanations. Avoid jargon. Be patient and helpful like a good teacher."
|
| 29 |
-
)
|
| 30 |
-
}
|
| 31 |
-
]
|
| 32 |
|
| 33 |
-
# Add chat history
|
| 34 |
-
for user_msg,
|
| 35 |
messages.append({"role": "user", "content": user_msg})
|
| 36 |
-
messages.append({"role": "assistant", "content":
|
| 37 |
|
|
|
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
|
|
|
| 40 |
try:
|
| 41 |
response = requests.post(
|
| 42 |
GROQ_API_URL,
|
| 43 |
-
headers=
|
| 44 |
json={
|
| 45 |
"model": MODEL,
|
| 46 |
-
"messages": messages
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
else:
|
| 53 |
-
return f"❌ Error: {response.status_code}\n{response.text}"
|
| 54 |
except Exception as e:
|
| 55 |
-
return f"
|
| 56 |
|
| 57 |
-
# Gradio UI
|
| 58 |
demo = gr.ChatInterface(
|
| 59 |
-
fn=
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
"What is a language model?",
|
| 65 |
-
"How does machine learning work?",
|
| 66 |
-
"Explain RAG in simple terms.",
|
| 67 |
-
"What is fine-tuning?"
|
| 68 |
-
],
|
| 69 |
-
description=(
|
| 70 |
-
"Ask me anything about LLMs, NLP, or AI! I'll explain in short, simple steps. "
|
| 71 |
-
"Great for beginners trying to learn how language models work."
|
| 72 |
-
)
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# Hugging Face
|
| 76 |
demo.launch(ssr_mode=False)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
# Load API key from .env or Hugging Face secret
|
| 7 |
load_dotenv()
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "").strip()
|
| 9 |
+
|
| 10 |
if not GROQ_API_KEY:
|
| 11 |
+
raise ValueError("Missing GROQ_API_KEY. Set it in your environment or Hugging Face secrets.")
|
| 12 |
|
| 13 |
+
# API Config
|
| 14 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 15 |
MODEL = "llama3-8b-8192"
|
| 16 |
+
HEADERS = {
|
| 17 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 18 |
"Content-Type": "application/json"
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Core chatbot logic
|
| 22 |
+
def chat(message, history):
|
| 23 |
+
messages = [{"role": "system", "content": "You are an expert AI tutor. Answer each question simply and clearly, using short and beginner-friendly explanations."}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Add full chat history
|
| 26 |
+
for user_msg, bot_msg in history:
|
| 27 |
messages.append({"role": "user", "content": user_msg})
|
| 28 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 29 |
|
| 30 |
+
# Add latest user message
|
| 31 |
messages.append({"role": "user", "content": message})
|
| 32 |
|
| 33 |
+
# Call Groq API
|
| 34 |
try:
|
| 35 |
response = requests.post(
|
| 36 |
GROQ_API_URL,
|
| 37 |
+
headers=HEADERS,
|
| 38 |
json={
|
| 39 |
"model": MODEL,
|
| 40 |
+
"messages": messages,
|
| 41 |
+
"temperature": 0.5,
|
| 42 |
+
"max_tokens": 500
|
| 43 |
}
|
| 44 |
)
|
| 45 |
+
response.raise_for_status()
|
| 46 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
| 47 |
+
return reply
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
+
return f"❌ Error: {str(e)}"
|
| 50 |
|
| 51 |
+
# Gradio UI
|
| 52 |
demo = gr.ChatInterface(
|
| 53 |
+
fn=chat,
|
| 54 |
+
title="🧠 LLM Mentor",
|
| 55 |
+
description="Learn about LLMs, NLP, and AI with clear, short answers from your friendly tutor.",
|
| 56 |
+
examples=["What is a language model?", "Explain NLP in simple words", "How does RAG work?"],
|
| 57 |
+
theme="soft",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# Disable SSR to prevent Hugging Face crash
|
| 61 |
demo.launch(ssr_mode=False)
|
| 62 |
|
| 63 |
+
|