ryanpereira commited on
Commit
5271f22
·
verified ·
1 Parent(s): 01a6781

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -47
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 environment variables
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 Hugging Face Secrets or .env.")
11
 
12
- # GROQ API setup
13
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
14
  MODEL = "llama3-8b-8192"
15
-
16
- headers = {
17
- "Authorization": f"Bearer {GROQ_API_KEY.strip()}",
18
  "Content-Type": "application/json"
19
  }
20
 
21
- # Core logic for handling chatbot responses
22
- def echo(message, history):
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 for context
34
- for user_msg, bot_reply in history:
35
  messages.append({"role": "user", "content": user_msg})
36
- messages.append({"role": "assistant", "content": bot_reply})
37
 
 
38
  messages.append({"role": "user", "content": message})
39
 
 
40
  try:
41
  response = requests.post(
42
  GROQ_API_URL,
43
- headers=headers,
44
  json={
45
  "model": MODEL,
46
- "messages": messages
 
 
47
  }
48
  )
49
-
50
- if response.status_code == 200:
51
- return response.json()["choices"][0]["message"]["content"]
52
- else:
53
- return f"❌ Error: {response.status_code}\n{response.text}"
54
  except Exception as e:
55
- return f"⚠️ Exception: {str(e)}"
56
 
57
- # Gradio UI setup
58
  demo = gr.ChatInterface(
59
- fn=echo,
60
- type="messages",
61
- return_messages=True,
62
- title="🤖 LLM Mentor - Your AI Tutor",
63
- examples=[
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 Spaces fix
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
+