Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import openai
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# Set up OpenAI API key (or use Hugging Face models if preferred)
|
| 7 |
+
openai.api_key = "YOUR_OPENAI_API_KEY"
|
| 8 |
+
|
| 9 |
+
# Define the study assistant chatbot
|
| 10 |
+
class StudyAssistantChatbot:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
# Use Hugging Face's Transformers pipeline or OpenAI for Q&A
|
| 13 |
+
self.qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 14 |
+
|
| 15 |
+
def get_chat_response(self, question: str):
|
| 16 |
+
# Query the chatbot model (OpenAI) to generate a response
|
| 17 |
+
response = openai.Completion.create(
|
| 18 |
+
engine="text-davinci-003",
|
| 19 |
+
prompt=f"Provide a study-relevant answer to the question: {question}",
|
| 20 |
+
max_tokens=150
|
| 21 |
+
)
|
| 22 |
+
return response.choices[0].text.strip()
|
| 23 |
+
|
| 24 |
+
def get_study_tips(self, subject: str):
|
| 25 |
+
# Placeholder for GROQ API call to fetch study resources
|
| 26 |
+
# Example: query GROQ API for study resources related to the subject
|
| 27 |
+
response = requests.get(f"https://api.groq.io/v1/resources?topic={subject}&api_key=YOUR_GROQ_API_KEY")
|
| 28 |
+
if response.status_code == 200:
|
| 29 |
+
resources = response.json()
|
| 30 |
+
return [resource['title'] + ": " + resource['url'] for resource in resources['data']]
|
| 31 |
+
else:
|
| 32 |
+
return ["Resource not available. Please check your API connection."]
|
| 33 |
+
|
| 34 |
+
def get_productivity_tips(self):
|
| 35 |
+
# Static productivity tips as an example
|
| 36 |
+
return [
|
| 37 |
+
"Break your study time into 25-minute blocks with 5-minute breaks.",
|
| 38 |
+
"Set specific, achievable study goals each day.",
|
| 39 |
+
"Limit distractions by turning off notifications.",
|
| 40 |
+
"Try to explain what you learned to someone else to reinforce understanding."
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
def get_practice_questions(self, topic: str):
|
| 44 |
+
# Example set of practice questions based on a topic
|
| 45 |
+
return [
|
| 46 |
+
f"Explain the key concepts of {topic}.",
|
| 47 |
+
f"List the important formulas related to {topic}.",
|
| 48 |
+
f"How would you apply {topic} in real-world scenarios?"
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
# Instantiate chatbot
|
| 52 |
+
chatbot = StudyAssistantChatbot()
|
| 53 |
+
|
| 54 |
+
# Streamlit app
|
| 55 |
+
st.title("Personalized Study Assistant Chatbot")
|
| 56 |
+
st.write("Ask me study-related questions, and I’ll provide tips, resources, and more!")
|
| 57 |
+
|
| 58 |
+
# Input fields for user queries and topics
|
| 59 |
+
query = st.text_input("Enter your study question:")
|
| 60 |
+
subject = st.text_input("Enter the subject you're studying:")
|
| 61 |
+
|
| 62 |
+
# Display response when the user submits a query
|
| 63 |
+
if st.button("Get Study Response"):
|
| 64 |
+
if query:
|
| 65 |
+
response = chatbot.get_chat_response(query)
|
| 66 |
+
st.write("### Chatbot Response:")
|
| 67 |
+
st.write(response)
|
| 68 |
+
|
| 69 |
+
if st.button("Get Study Tips"):
|
| 70 |
+
if subject:
|
| 71 |
+
study_tips = chatbot.get_study_tips(subject)
|
| 72 |
+
st.write("### Study Tips:")
|
| 73 |
+
for tip in study_tips:
|
| 74 |
+
st.write(f"- {tip}")
|
| 75 |
+
else:
|
| 76 |
+
st.write("Please enter a subject to get study tips.")
|
| 77 |
+
|
| 78 |
+
if st.button("Get Productivity Tips"):
|
| 79 |
+
productivity_tips = chatbot.get_productivity_tips()
|
| 80 |
+
st.write("### Productivity Tips:")
|
| 81 |
+
for tip in productivity_tips:
|
| 82 |
+
st.write(f"- {tip}")
|
| 83 |
+
|
| 84 |
+
if st.button("Get Practice Questions"):
|
| 85 |
+
if subject:
|
| 86 |
+
practice_questions = chatbot.get_practice_questions(subject)
|
| 87 |
+
st.write("### Practice Questions:")
|
| 88 |
+
for question in practice_questions:
|
| 89 |
+
st.write(f"- {question}")
|
| 90 |
+
else:
|
| 91 |
+
st.write("Please enter a subject to get practice questions.")
|