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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -18
app.py CHANGED
@@ -1,31 +1,40 @@
1
- # app.py
2
  import gradio as gr
3
  import requests
4
  import os
5
  from dotenv import load_dotenv
6
 
7
- load_dotenv() # Load .env if running locally
8
-
9
- GROQ_API_KEY = os.getenv("GROQ_API_KEY", "").strip()
10
  if not GROQ_API_KEY:
11
- raise ValueError("Missing GROQ_API_KEY. Set it in Hugging Face Secrets or a .env file.")
12
 
 
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}",
18
  "Content-Type": "application/json"
19
  }
20
 
 
21
  def echo(message, history):
22
- messages = [{"role": "system", "content": "You are a helpful LLM teacher."}]
23
-
24
- # Add chat history for better context
25
- for user, bot in history:
26
- messages.append({"role": "user", "content": user})
27
- messages.append({"role": "assistant", "content": bot})
28
-
 
 
 
 
 
 
 
 
29
  messages.append({"role": "user", "content": message})
30
 
31
  try:
@@ -41,16 +50,28 @@ def echo(message, history):
41
  if response.status_code == 200:
42
  return response.json()["choices"][0]["message"]["content"]
43
  else:
44
- return f"Error: {response.status_code}\n{response.text}"
45
  except Exception as e:
46
- return f"Exception occurred: {str(e)}"
47
 
 
48
  demo = gr.ChatInterface(
49
  fn=echo,
50
  type="messages",
51
- examples=["I want to learn about LLMs", "What is NLP?", "What is RAG?"],
52
- title="LLM Mentor"
 
 
 
 
 
 
 
 
 
 
53
  )
54
 
55
- # Disable SSR to avoid Hugging Face crash
56
  demo.launch(ssr_mode=False)
 
 
 
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:
 
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
+