Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
import openai
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
OPENAI_API_KEY = "sk-...1-AA" # Replace with your actual OpenAI API key
|
| 9 |
-
|
| 10 |
-
# Authenticate with OpenAI API
|
| 11 |
-
openai.api_key = OPENAI_API_KEY
|
| 12 |
|
| 13 |
# Function to get responses from OpenAI
|
| 14 |
def get_chat_response(query):
|
|
@@ -18,24 +14,18 @@ def get_chat_response(query):
|
|
| 18 |
{"role": "user", "content": query}
|
| 19 |
]
|
| 20 |
)
|
| 21 |
-
return response
|
| 22 |
|
| 23 |
-
# Class for the local chatbot using Hugging Face Transformers
|
| 24 |
class StudyAssistantChatbot:
|
| 25 |
def __init__(self):
|
| 26 |
-
#
|
| 27 |
try:
|
| 28 |
-
self.qa_pipeline = pipeline("text-generation", model="distilgpt2"
|
| 29 |
except RuntimeError as e:
|
| 30 |
st.error(f"Error loading the model: {e}")
|
| 31 |
-
st.error("Please
|
| 32 |
raise
|
| 33 |
|
| 34 |
-
def generate_study_tips(self, query):
|
| 35 |
-
# Generate study tips using the local model
|
| 36 |
-
response = self.qa_pipeline(query, max_length=50, num_return_sequences=1)
|
| 37 |
-
return response[0]['generated_text']
|
| 38 |
-
|
| 39 |
# Initialize Streamlit app
|
| 40 |
st.title("Personalized Study Assistant Chatbot")
|
| 41 |
|
|
@@ -52,22 +42,11 @@ if st.button("Get Tips and Resources"):
|
|
| 52 |
if query:
|
| 53 |
# Get response from OpenAI
|
| 54 |
response = get_chat_response(query)
|
| 55 |
-
|
| 56 |
-
st.subheader("OpenAI GPT Response:")
|
| 57 |
-
st.write(response)
|
| 58 |
-
else:
|
| 59 |
-
st.write("Unable to get a response from OpenAI at the moment.")
|
| 60 |
-
|
| 61 |
-
# Get study tips from the local model
|
| 62 |
-
st.subheader("Study Tips from Local Model:")
|
| 63 |
-
tips_response = chatbot.generate_study_tips(query)
|
| 64 |
-
if tips_response:
|
| 65 |
-
st.write(tips_response)
|
| 66 |
-
else:
|
| 67 |
-
st.write("Unable to generate study tips at the moment.")
|
| 68 |
else:
|
| 69 |
st.write("Please enter a question to get started!")
|
| 70 |
|
| 71 |
-
# Add a sidebar for additional
|
| 72 |
st.sidebar.header("About")
|
| 73 |
-
st.sidebar.text("This is a personalized study assistant chatbot
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
import openai
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
+
# Initialize OpenAI API key
|
| 7 |
+
openai.api_key = "sk-...1-AA" # Replace with your actual OpenAI API key
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Function to get responses from OpenAI
|
| 10 |
def get_chat_response(query):
|
|
|
|
| 14 |
{"role": "user", "content": query}
|
| 15 |
]
|
| 16 |
)
|
| 17 |
+
return response['choices'][0]['message']['content']
|
| 18 |
|
|
|
|
| 19 |
class StudyAssistantChatbot:
|
| 20 |
def __init__(self):
|
| 21 |
+
# Check if either TensorFlow or PyTorch is installed
|
| 22 |
try:
|
| 23 |
+
self.qa_pipeline = pipeline("text-generation", model="distilgpt2")
|
| 24 |
except RuntimeError as e:
|
| 25 |
st.error(f"Error loading the model: {e}")
|
| 26 |
+
st.error("Please make sure either TensorFlow or PyTorch is installed.")
|
| 27 |
raise
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Initialize Streamlit app
|
| 30 |
st.title("Personalized Study Assistant Chatbot")
|
| 31 |
|
|
|
|
| 42 |
if query:
|
| 43 |
# Get response from OpenAI
|
| 44 |
response = get_chat_response(query)
|
| 45 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
else:
|
| 47 |
st.write("Please enter a question to get started!")
|
| 48 |
|
| 49 |
+
# Add a sidebar for additional options
|
| 50 |
st.sidebar.header("About")
|
| 51 |
+
st.sidebar.text("This is a personalized study assistant chatbot.")
|
| 52 |
+
|