Spaces:
Sleeping
Sleeping
shrivarshan
commited on
Commit
•
249b997
1
Parent(s):
1affdba
Upload 2 files
Browse files- app.py +129 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from gtts import gTTS
|
4 |
+
from fpdf import FPDF
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load pipelines with correct models
|
8 |
+
text_generator = pipeline("text-generation", model="gpt2")
|
9 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
10 |
+
qa_generator = pipeline("text2text-generation", model="valhalla/t5-small-qg-hl")
|
11 |
+
|
12 |
+
# Sample Q/A for demonstration
|
13 |
+
sample_qa = {
|
14 |
+
"Question": "What is the process of photosynthesis?",
|
15 |
+
"Answer": "Photosynthesis is the process by which green plants and some other organisms use sunlight to synthesize foods with the help of chlorophyll."
|
16 |
+
}
|
17 |
+
|
18 |
+
st.title("GenAI-Powered Student Exam Preparation Assistant")
|
19 |
+
|
20 |
+
# Sidebar for topic selection
|
21 |
+
st.sidebar.title("Options")
|
22 |
+
selected_task = st.sidebar.selectbox("Select Task", ["Explain Topic", "View Question Bank", "Sample Q/A", "Take Test"])
|
23 |
+
|
24 |
+
def generate_explanation(topic):
|
25 |
+
prompt = f"Provide a detailed explanation of the following topic: {topic} in simple terms."
|
26 |
+
explanation = text_generator(prompt, max_length=500, num_return_sequences=1, temperature=0.7)
|
27 |
+
return explanation[0]['generated_text']
|
28 |
+
|
29 |
+
def generate_quiz(explanation):
|
30 |
+
prompt = f"Generate a quiz question based on the following content: {explanation}"
|
31 |
+
quiz_question = qa_generator(prompt, max_length=150, num_return_sequences=1)
|
32 |
+
return quiz_question[0]['generated_text']
|
33 |
+
|
34 |
+
def text_to_speech(text):
|
35 |
+
tts = gTTS(text=text, lang='en')
|
36 |
+
audio_file = "explanation.mp3"
|
37 |
+
tts.save(audio_file)
|
38 |
+
return audio_file
|
39 |
+
|
40 |
+
if selected_task == "Explain Topic":
|
41 |
+
st.header("Topic Explanation")
|
42 |
+
topic = st.text_input("Enter the topic you want explained:", "")
|
43 |
+
|
44 |
+
if st.button("Generate Explanation"):
|
45 |
+
if topic:
|
46 |
+
explanation = generate_explanation(topic)
|
47 |
+
st.subheader("Explanation:")
|
48 |
+
st.write(explanation)
|
49 |
+
|
50 |
+
# Generate and display audio
|
51 |
+
audio_file = text_to_speech(explanation)
|
52 |
+
st.audio(audio_file, format='audio/mp3')
|
53 |
+
|
54 |
+
# Add the Quiz button only after the explanation is shown
|
55 |
+
if st.button("Generate Quiz Questions"):
|
56 |
+
quiz_question = generate_quiz(explanation)
|
57 |
+
st.subheader("Quiz Question:")
|
58 |
+
st.write(quiz_question)
|
59 |
+
else:
|
60 |
+
st.warning("Please enter a topic to explain.")
|
61 |
+
|
62 |
+
elif selected_task == "View Question Bank":
|
63 |
+
st.header("Question Bank")
|
64 |
+
st.write("Feature to view and manage the question bank will be added here.")
|
65 |
+
|
66 |
+
elif selected_task == "Sample Q/A":
|
67 |
+
st.header("Sample Question and Answer")
|
68 |
+
st.write("**Question:**")
|
69 |
+
st.write(sample_qa["Question"])
|
70 |
+
st.write("**Answer:**")
|
71 |
+
st.write(sample_qa["Answer"])
|
72 |
+
|
73 |
+
elif selected_task == "Take Test":
|
74 |
+
st.header("AI-Proctored Test")
|
75 |
+
st.write("This is a simulated AI-proctored test environment.")
|
76 |
+
|
77 |
+
# Example questions for the test
|
78 |
+
questions = [
|
79 |
+
"What is the capital of France?",
|
80 |
+
"Explain the law of demand.",
|
81 |
+
"Describe the water cycle."
|
82 |
+
]
|
83 |
+
|
84 |
+
user_answers = []
|
85 |
+
|
86 |
+
for i, question in enumerate(questions):
|
87 |
+
st.subheader(f"Question {i + 1}: {question}")
|
88 |
+
answer = st.text_input(f"Your Answer for Question {i + 1}", key=f"answer_{i}")
|
89 |
+
user_answers.append(answer)
|
90 |
+
|
91 |
+
# Start the camera feed using HTML and JavaScript
|
92 |
+
st.write("### Please allow camera access for proctoring.")
|
93 |
+
st.markdown("""
|
94 |
+
<video id="video" width="400" height="300" autoplay></video>
|
95 |
+
<script>
|
96 |
+
const video = document.getElementById('video');
|
97 |
+
navigator.mediaDevices.getUserMedia({ video: true })
|
98 |
+
.then(stream => {
|
99 |
+
video.srcObject = stream;
|
100 |
+
})
|
101 |
+
.catch(err => {
|
102 |
+
console.error("Error accessing webcam: ", err);
|
103 |
+
});
|
104 |
+
</script>
|
105 |
+
""", unsafe_allow_html=True)
|
106 |
+
|
107 |
+
if st.button("Submit Answers"):
|
108 |
+
# Generate a PDF from the user's answers
|
109 |
+
pdf = FPDF()
|
110 |
+
pdf.add_page()
|
111 |
+
pdf.set_font("Arial", size=12)
|
112 |
+
|
113 |
+
for i, answer in enumerate(user_answers):
|
114 |
+
pdf.cell(200, 10, txt=f"Question {i + 1}: {questions[i]}", ln=True)
|
115 |
+
pdf.cell(200, 10, txt=f"Your Answer: {answer}", ln=True)
|
116 |
+
pdf.cell(200, 10, txt="", ln=True) # Add a blank line for spacing
|
117 |
+
|
118 |
+
pdf_file_path = "user_answers.pdf"
|
119 |
+
pdf.output(pdf_file_path)
|
120 |
+
|
121 |
+
st.success("Your answers have been submitted and saved to PDF!")
|
122 |
+
|
123 |
+
# Provide the PDF for download
|
124 |
+
with open(pdf_file_path, "rb") as f:
|
125 |
+
st.download_button("Download Your Answers PDF", f, file_name=pdf_file_path)
|
126 |
+
|
127 |
+
# Ensure to clean up any generated audio files
|
128 |
+
if os.path.exists("explanation.mp3"):
|
129 |
+
os.remove("explanation.mp3")
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
transformers
|
3 |
+
tokenizers
|
4 |
+
gtts
|
5 |
+
fpdf
|
6 |
+
pyngrok
|