Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,23 @@ import streamlit as st
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import openai
|
| 4 |
import requests
|
|
|
|
| 5 |
|
| 6 |
-
# Set up OpenAI API key (
|
| 7 |
-
openai.api_key = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Define the study assistant chatbot
|
| 10 |
class StudyAssistantChatbot:
|
| 11 |
def __init__(self):
|
| 12 |
-
# Use Hugging Face's Transformers pipeline
|
| 13 |
-
self.qa_pipeline = pipeline("
|
| 14 |
-
|
| 15 |
def get_chat_response(self, question: str):
|
| 16 |
-
# Query
|
| 17 |
response = openai.Completion.create(
|
| 18 |
engine="text-davinci-003",
|
| 19 |
prompt=f"Provide a study-relevant answer to the question: {question}",
|
|
@@ -22,38 +27,36 @@ class StudyAssistantChatbot:
|
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
resources = response.json()
|
| 30 |
-
return [resource['title']
|
| 31 |
-
|
| 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
|
| 38 |
"Set specific, achievable study goals each day.",
|
| 39 |
"Limit distractions by turning off notifications.",
|
| 40 |
-
"
|
| 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
|
| 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
|
| 57 |
|
| 58 |
# Input fields for user queries and topics
|
| 59 |
query = st.text_input("Enter your study question:")
|
|
@@ -88,4 +91,4 @@ if st.button("Get 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.")
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import openai
|
| 4 |
import requests
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
# Set up OpenAI API key (replace with your actual API key)
|
| 8 |
+
openai.api_key = "sk-...yRIA" # Replace with your actual OpenAI API key
|
| 9 |
+
|
| 10 |
+
# Check if PyTorch or TensorFlow is installed, required by transformers
|
| 11 |
+
if not torch.cuda.is_available() and not torch.backends.mps.is_available():
|
| 12 |
+
st.warning("Note: The CPU version of PyTorch is installed. For better performance, use a system with CUDA-enabled GPU.")
|
| 13 |
|
| 14 |
# Define the study assistant chatbot
|
| 15 |
class StudyAssistantChatbot:
|
| 16 |
def __init__(self):
|
| 17 |
+
# Use Hugging Face's Transformers pipeline with a lightweight model
|
| 18 |
+
self.qa_pipeline = pipeline("text-generation", model="distilgpt2", framework="pt")
|
| 19 |
+
|
| 20 |
def get_chat_response(self, question: str):
|
| 21 |
+
# Query OpenAI to generate a response
|
| 22 |
response = openai.Completion.create(
|
| 23 |
engine="text-davinci-003",
|
| 24 |
prompt=f"Provide a study-relevant answer to the question: {question}",
|
|
|
|
| 27 |
return response.choices[0].text.strip()
|
| 28 |
|
| 29 |
def get_study_tips(self, subject: str):
|
| 30 |
+
# Placeholder for a GROQ API call to fetch study resources
|
| 31 |
+
try:
|
| 32 |
+
response = requests.get(f"studybot") # Replace with your GROQ API key
|
| 33 |
+
response.raise_for_status()
|
| 34 |
resources = response.json()
|
| 35 |
+
return [f"{resource['title']}: {resource['url']}" for resource in resources['data']]
|
| 36 |
+
except requests.RequestException:
|
| 37 |
return ["Resource not available. Please check your API connection."]
|
| 38 |
|
| 39 |
def get_productivity_tips(self):
|
|
|
|
| 40 |
return [
|
| 41 |
+
"Break study time into 25-minute blocks with 5-minute breaks.",
|
| 42 |
"Set specific, achievable study goals each day.",
|
| 43 |
"Limit distractions by turning off notifications.",
|
| 44 |
+
"Explain what you learned to someone else to reinforce understanding."
|
| 45 |
]
|
| 46 |
|
| 47 |
def get_practice_questions(self, topic: str):
|
|
|
|
| 48 |
return [
|
| 49 |
f"Explain the key concepts of {topic}.",
|
| 50 |
+
f"List important formulas related to {topic}.",
|
| 51 |
f"How would you apply {topic} in real-world scenarios?"
|
| 52 |
]
|
| 53 |
|
| 54 |
+
# Instantiate the chatbot
|
| 55 |
chatbot = StudyAssistantChatbot()
|
| 56 |
|
| 57 |
# Streamlit app
|
| 58 |
st.title("Personalized Study Assistant Chatbot")
|
| 59 |
+
st.write("Ask study-related questions, get tips, and find resources!")
|
| 60 |
|
| 61 |
# Input fields for user queries and topics
|
| 62 |
query = st.text_input("Enter your study question:")
|
|
|
|
| 91 |
for question in practice_questions:
|
| 92 |
st.write(f"- {question}")
|
| 93 |
else:
|
| 94 |
+
st.write("Please enter a subject to get practice questions.")
|