Muzamilahmed commited on
Commit
be10bd3
Β·
verified Β·
1 Parent(s): df0c2e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -1,17 +1,16 @@
1
  import streamlit as st
2
  import os
3
- import openai
4
  from dotenv import load_dotenv
5
 
6
- # Load API key from .env file
7
  load_dotenv()
8
- openai.api_key = os.getenv("GROQ_API_KEY")
9
 
10
  st.set_page_config(page_title="EduGuide Chatbot", page_icon="πŸŽ“")
11
  st.title("πŸŽ“ EduGuide Chatbot")
12
  st.write("Ask me anything related to education. For other queries, I will politely decline.")
13
 
14
- # Define keywords to identify education-related queries
15
  def is_education_related(prompt):
16
  education_keywords = [
17
  "education", "university", "college", "admission", "exam", "study", "learning", "subject",
@@ -19,24 +18,33 @@ def is_education_related(prompt):
19
  ]
20
  return any(word in prompt.lower() for word in education_keywords)
21
 
22
- # Query the LLaMA model via Groq
23
  def ask_llama(prompt):
24
- response = openai.ChatCompletion.create(
25
- model="llama3-70b-8192",
26
- messages=[
 
 
 
 
 
27
  {"role": "system", "content": "You are an education assistant. Only answer education-related questions."},
28
  {"role": "user", "content": prompt}
29
- ]
30
- )
31
- return response['choices'][0]['message']['content']
 
 
 
32
 
33
- # Streamlit input/output
34
  user_input = st.text_input("πŸ’¬ Ask your question:")
35
  if st.button("Ask"):
36
  if user_input:
37
  if is_education_related(user_input):
38
  with st.spinner("Thinking..."):
39
- answer = ask_llama(user_input)
40
- st.success(answer)
 
 
 
41
  else:
42
  st.warning("Sorry, I do not have knowledge of it.")
 
1
  import streamlit as st
2
  import os
3
+ import requests
4
  from dotenv import load_dotenv
5
 
6
+ # Load API key
7
  load_dotenv()
8
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
9
 
10
  st.set_page_config(page_title="EduGuide Chatbot", page_icon="πŸŽ“")
11
  st.title("πŸŽ“ EduGuide Chatbot")
12
  st.write("Ask me anything related to education. For other queries, I will politely decline.")
13
 
 
14
  def is_education_related(prompt):
15
  education_keywords = [
16
  "education", "university", "college", "admission", "exam", "study", "learning", "subject",
 
18
  ]
19
  return any(word in prompt.lower() for word in education_keywords)
20
 
 
21
  def ask_llama(prompt):
22
+ url = "https://api.groq.com/openai/v1/chat/completions"
23
+ headers = {
24
+ "Authorization": f"Bearer {GROQ_API_KEY}",
25
+ "Content-Type": "application/json"
26
+ }
27
+ data = {
28
+ "model": "llama3-70b-8192",
29
+ "messages": [
30
  {"role": "system", "content": "You are an education assistant. Only answer education-related questions."},
31
  {"role": "user", "content": prompt}
32
+ ],
33
+ "temperature": 0.7
34
+ }
35
+ response = requests.post(url, headers=headers, json=data)
36
+ result = response.json()
37
+ return result["choices"][0]["message"]["content"]
38
 
 
39
  user_input = st.text_input("πŸ’¬ Ask your question:")
40
  if st.button("Ask"):
41
  if user_input:
42
  if is_education_related(user_input):
43
  with st.spinner("Thinking..."):
44
+ try:
45
+ answer = ask_llama(user_input)
46
+ st.success(answer)
47
+ except Exception as e:
48
+ st.error(f"❌ Error: {e}")
49
  else:
50
  st.warning("Sorry, I do not have knowledge of it.")