Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
# Load API key
|
| 7 |
load_dotenv()
|
| 8 |
-
|
| 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 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
{"role": "system", "content": "You are an education assistant. Only answer education-related questions."},
|
| 28 |
{"role": "user", "content": prompt}
|
| 29 |
-
]
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 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.")
|