Spaces:
Running
Running
Upload 4 files
Browse files- app.py +211 -0
- hackathon_api.py +42 -0
- requirements.txt +2 -0
- use_api.py +10 -0
app.py
ADDED
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import hackathon_api as api
|
3 |
+
import sqlite3
|
4 |
+
from fpdf import FPDF
|
5 |
+
|
6 |
+
# Create or connect to the SQLite database
|
7 |
+
conn = sqlite3.connect('user.db')
|
8 |
+
c = conn.cursor()
|
9 |
+
|
10 |
+
# Create a table to store user information if it doesn't exist
|
11 |
+
c.execute('''CREATE TABLE IF NOT EXISTS users
|
12 |
+
(username TEXT PRIMARY KEY, password TEXT)''')
|
13 |
+
conn.commit()
|
14 |
+
|
15 |
+
# Create a table to store user quiz scores if it doesn't exist
|
16 |
+
c.execute('''CREATE TABLE IF NOT EXISTS quiz_scores
|
17 |
+
(username TEXT, course TEXT, score INTEGER,
|
18 |
+
FOREIGN KEY(username) REFERENCES users(username))''')
|
19 |
+
conn.commit()
|
20 |
+
|
21 |
+
# Function to check if a username already exists
|
22 |
+
def username_exists(username):
|
23 |
+
c.execute("SELECT * FROM users WHERE username=?", (username,))
|
24 |
+
return c.fetchone() is not None
|
25 |
+
|
26 |
+
# Function to authenticate user
|
27 |
+
def authenticate_user(username, password):
|
28 |
+
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
29 |
+
return c.fetchone() is not None
|
30 |
+
|
31 |
+
# Function to generate PDF
|
32 |
+
def generate_pdf(content):
|
33 |
+
pdf = FPDF()
|
34 |
+
pdf.add_page()
|
35 |
+
pdf.set_font("Arial", size=12)
|
36 |
+
pdf.multi_cell(0, 10, content)
|
37 |
+
|
38 |
+
pdf_output = pdf.output(dest='S').encode('latin1') # 'S' means output as a string
|
39 |
+
return pdf_output
|
40 |
+
|
41 |
+
# Function to handle course content and PDF generation/download
|
42 |
+
def course_content_ui():
|
43 |
+
st.title("Course Content")
|
44 |
+
|
45 |
+
st.header("Enter Topic")
|
46 |
+
topic = st.text_input("Enter topic here")
|
47 |
+
|
48 |
+
if topic:
|
49 |
+
st.subheader("Course Content for {}".format(topic))
|
50 |
+
course_content = api.generate_cours(topic)
|
51 |
+
st.write(course_content)
|
52 |
+
pdf_data = generate_pdf(course_content)
|
53 |
+
st.download_button(
|
54 |
+
label="Download PDF",
|
55 |
+
data=pdf_data,
|
56 |
+
file_name="course_content.pdf",
|
57 |
+
mime="application/pdf",
|
58 |
+
key="download_pdf_button"
|
59 |
+
)
|
60 |
+
|
61 |
+
# Function to handle chat about confusion
|
62 |
+
def chat_ui():
|
63 |
+
st.title("Chat Your Confusions")
|
64 |
+
|
65 |
+
# Initialize chat history for chat tab
|
66 |
+
if "chat_messages" not in st.session_state:
|
67 |
+
st.session_state.chat_messages = []
|
68 |
+
|
69 |
+
# Display chat messages from history on a container
|
70 |
+
for message in st.session_state.chat_messages:
|
71 |
+
with st.chat_message(message["role"]):
|
72 |
+
st.markdown(message["content"])
|
73 |
+
|
74 |
+
# React to user input
|
75 |
+
if prompt := st.chat_input("What is your confusion?"):
|
76 |
+
# Display user message in chat message container
|
77 |
+
with st.chat_message("user"):
|
78 |
+
st.markdown(prompt)
|
79 |
+
# Add user message to chat history
|
80 |
+
st.session_state.chat_messages.append({"role": "user", "content": prompt})
|
81 |
+
|
82 |
+
response = f"Assistant: It seems like you're confused about '{prompt}'. Let's clarify."
|
83 |
+
# Display assistant response in chat message container
|
84 |
+
with st.chat_message("assistant"):
|
85 |
+
st.markdown(response)
|
86 |
+
# Add assistant response to chat history
|
87 |
+
st.session_state.chat_messages.append({"role": "assistant", "content": response})
|
88 |
+
|
89 |
+
# Function to handle quiz chat and store the score
|
90 |
+
def quiz_ui():
|
91 |
+
st.title("Start the Quiz")
|
92 |
+
|
93 |
+
# Initialize chat history for quiz tab
|
94 |
+
if "quiz_messages" not in st.session_state:
|
95 |
+
st.session_state.quiz_messages = []
|
96 |
+
|
97 |
+
# Display chat messages from history on a container
|
98 |
+
for message in st.session_state.quiz_messages:
|
99 |
+
with st.chat_message(message["role"]):
|
100 |
+
st.markdown(message["content"])
|
101 |
+
|
102 |
+
# React to user input
|
103 |
+
if prompt := st.chat_input("Ready for a quiz question?"):
|
104 |
+
# Display user message in chat message container
|
105 |
+
with st.chat_message("user"):
|
106 |
+
st.markdown(prompt)
|
107 |
+
# Add user message to chat history
|
108 |
+
st.session_state.quiz_messages.append({"role": "user", "content": prompt})
|
109 |
+
|
110 |
+
response = "Quiz Question: What is the capital of France?"
|
111 |
+
correct_answer = "Paris"
|
112 |
+
|
113 |
+
# Display assistant response in chat message container
|
114 |
+
with st.chat_message("assistant"):
|
115 |
+
st.markdown(response)
|
116 |
+
|
117 |
+
if prompt.lower() == correct_answer.lower():
|
118 |
+
score = 100
|
119 |
+
else:
|
120 |
+
score = 0
|
121 |
+
|
122 |
+
# Store the quiz score in the database
|
123 |
+
c.execute("INSERT INTO quiz_scores (username, course, score) VALUES (?, ?, ?)",
|
124 |
+
(st.session_state.username, "Geography", score))
|
125 |
+
conn.commit()
|
126 |
+
|
127 |
+
st.session_state.quiz_messages.append({"role": "assistant", "content": response})
|
128 |
+
st.session_state.quiz_messages.append({"role": "assistant", "content": f"Your score: {score}/100"})
|
129 |
+
|
130 |
+
# Function to display user info and quiz scores
|
131 |
+
def user_info_ui():
|
132 |
+
st.title("User Info")
|
133 |
+
|
134 |
+
c.execute("SELECT * FROM users WHERE username=?", (st.session_state.username,))
|
135 |
+
user_info = c.fetchone()
|
136 |
+
|
137 |
+
if user_info:
|
138 |
+
st.write(f"Username: {user_info[0]}")
|
139 |
+
|
140 |
+
st.subheader("Quiz Scores")
|
141 |
+
c.execute("SELECT course, score FROM quiz_scores WHERE username=?", (st.session_state.username,))
|
142 |
+
scores = c.fetchall()
|
143 |
+
|
144 |
+
if scores:
|
145 |
+
for course, score in scores:
|
146 |
+
st.write(f"Course: {course}, Score: {score}/100")
|
147 |
+
else:
|
148 |
+
st.write("No quiz scores available.")
|
149 |
+
else:
|
150 |
+
st.write("User information not available.")
|
151 |
+
|
152 |
+
# Logged-in UI
|
153 |
+
def logged_in_ui(username):
|
154 |
+
st.sidebar.title("Navigation")
|
155 |
+
tab = st.sidebar.radio("Select a tab", ("Course Content", "Chat", "Quiz Chat", "User Info"))
|
156 |
+
|
157 |
+
if tab == "Course Content":
|
158 |
+
course_content_ui()
|
159 |
+
elif tab == "Chat":
|
160 |
+
chat_ui()
|
161 |
+
elif tab == "Quiz Chat":
|
162 |
+
quiz_ui()
|
163 |
+
elif tab == "User Info":
|
164 |
+
user_info_ui()
|
165 |
+
|
166 |
+
# Main function to handle login, registration, and logged-in UI
|
167 |
+
def main():
|
168 |
+
if 'logged_in' not in st.session_state:
|
169 |
+
st.session_state.logged_in = False
|
170 |
+
st.session_state.username = ""
|
171 |
+
|
172 |
+
if st.session_state.logged_in:
|
173 |
+
logged_in_ui(st.session_state.username)
|
174 |
+
else:
|
175 |
+
st.sidebar.title("Navigation")
|
176 |
+
page = st.sidebar.radio("Go to", ("Login", "Register"))
|
177 |
+
|
178 |
+
if page == "Login":
|
179 |
+
st.title("Login")
|
180 |
+
username = st.text_input("Username")
|
181 |
+
password = st.text_input("Password", type="password")
|
182 |
+
|
183 |
+
if st.button("Login"):
|
184 |
+
if authenticate_user(username, password):
|
185 |
+
st.session_state.logged_in = True
|
186 |
+
st.session_state.username = username
|
187 |
+
st.experimental_rerun()
|
188 |
+
else:
|
189 |
+
st.error("Invalid username or password")
|
190 |
+
|
191 |
+
elif page == "Register":
|
192 |
+
st.title("Register")
|
193 |
+
new_username = st.text_input("New Username")
|
194 |
+
new_password = st.text_input("New Password", type="password")
|
195 |
+
confirm_password = st.text_input("Confirm Password", type="password")
|
196 |
+
|
197 |
+
if st.button("Register"):
|
198 |
+
if new_password != confirm_password:
|
199 |
+
st.error("Passwords do not match")
|
200 |
+
elif username_exists(new_username):
|
201 |
+
st.error("Username already exists. Please choose another one.")
|
202 |
+
else:
|
203 |
+
c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (new_username, new_password))
|
204 |
+
conn.commit()
|
205 |
+
st.success("Registration successful for {}".format(new_username))
|
206 |
+
st.session_state.logged_in = True
|
207 |
+
st.session_state.username = new_username
|
208 |
+
st.experimental_rerun()
|
209 |
+
|
210 |
+
if __name__ == "__main__":
|
211 |
+
main()
|
hackathon_api.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import replicate
|
2 |
+
import os
|
3 |
+
|
4 |
+
|
5 |
+
os.environ['REPLICATE_API_TOKEN'] = "r8_8tgu21LprONdmHOqlZciw9gn1qwrLuz2u9qg9"
|
6 |
+
|
7 |
+
use_model = "a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5"
|
8 |
+
|
9 |
+
def prompt_runer(prompt):
|
10 |
+
try:
|
11 |
+
output_generator = replicate.run(
|
12 |
+
use_model,
|
13 |
+
input={"prompt": prompt}
|
14 |
+
)
|
15 |
+
# Collect the outputs from the generator
|
16 |
+
output = "".join(output_generator)
|
17 |
+
|
18 |
+
except replicate.exceptions.ReplicateError as e:
|
19 |
+
print(f"Replicate error: {e}")
|
20 |
+
output = "Error in generation of reponse"
|
21 |
+
except Exception as e:
|
22 |
+
print(f"Unexpected error: {e}")
|
23 |
+
#raise
|
24 |
+
output = "Error in generation of reponse"
|
25 |
+
return output
|
26 |
+
def generate_cours(topic):
|
27 |
+
prompt_eng = f"Generate me a well-written course on: {topic}"
|
28 |
+
return prompt_runer(prompt_eng)
|
29 |
+
|
30 |
+
#Function for generating the cours questions which takes the theme
|
31 |
+
def generate_evaluation_questions(course_content):
|
32 |
+
prompt_eng = f"Based on the following course content, generate 10 evaluation questions:\n\n{course_content}"
|
33 |
+
return prompt_runer(prompt_eng)
|
34 |
+
|
35 |
+
#Function for generating the question answers which takes the theme and user question
|
36 |
+
def answer_question(course_content, user_question):
|
37 |
+
prompt_eng = f"Based on the following course content, answer the user's question:\n\nCourse content: {course_content}\n\nUser's question: {user_question}",
|
38 |
+
return prompt_runer(prompt_eng)
|
39 |
+
|
40 |
+
|
41 |
+
topic = "explain me the force in physics"
|
42 |
+
print(generate_cours(topic))
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
fpdf
|
2 |
+
replicate
|
use_api.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hackathon_api as api
|
2 |
+
|
3 |
+
|
4 |
+
topic = "explain me the force in physics"
|
5 |
+
course_content = api.generate_cours(topic)
|
6 |
+
|
7 |
+
api.generate_evaluation_questions(course_content)
|
8 |
+
|
9 |
+
user_question = "Define acceleration for me"
|
10 |
+
api.answer_question(course_content, user_question)
|