Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,118 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
-
import os
|
| 4 |
import random
|
| 5 |
-
import
|
| 6 |
|
| 7 |
# Set your OpenAI API key
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
"""
|
| 12 |
-
Generate MCQs dynamically using OpenAI's GPT.
|
| 13 |
-
Returns HTML with checkboxes and Show Answers button.
|
| 14 |
-
"""
|
| 15 |
-
prompt = f"""
|
| 16 |
-
Generate {num_questions} multiple-choice questions on "{topic}".
|
| 17 |
-
Format the output as follows for each question:
|
| 18 |
-
Question: ...
|
| 19 |
-
Options: A) ... B) ... C) ... D) ...
|
| 20 |
-
Answer: ...
|
| 21 |
-
"""
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
response = openai.Completion.create(
|
| 24 |
engine="text-davinci-003",
|
| 25 |
prompt=prompt,
|
| 26 |
-
max_tokens=
|
| 27 |
temperature=0.7
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
html += f"<
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
html += """
|
| 47 |
-
<button onclick="document.querySelectorAll('[id^=ans]').forEach(a=>a.style.display='block');"
|
| 48 |
-
style='margin-top:10px;background:#7c3aed;color:white;border:none;padding:10px 20px;border-radius:8px;cursor:pointer;'>
|
| 49 |
-
Show Answers
|
| 50 |
-
</button>
|
| 51 |
-
</div>
|
| 52 |
-
"""
|
| 53 |
return html
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
|
|
|
| 3 |
import random
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# Set your OpenAI API key
|
| 7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
+
# -------------------- AI Functions --------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def chat_tutor(message, history=[]):
|
| 12 |
+
"""AI Study Tutor Chat"""
|
| 13 |
+
prompt = f"You are EduAI, a smart student-friendly tutor. Answer clearly and concisely:\n\nUser: {message}\nAI:"
|
| 14 |
response = openai.Completion.create(
|
| 15 |
engine="text-davinci-003",
|
| 16 |
prompt=prompt,
|
| 17 |
+
max_tokens=200,
|
| 18 |
temperature=0.7
|
| 19 |
)
|
| 20 |
+
reply = response.choices[0].text.strip()
|
| 21 |
+
history.append((message, reply))
|
| 22 |
+
return history, history
|
| 23 |
|
| 24 |
+
def generate_mcqs(topic, num_questions=3):
|
| 25 |
+
"""AI generates MCQs dynamically"""
|
| 26 |
+
prompt = f"Create {num_questions} multiple choice questions on '{topic}' with 4 options (A,B,C,D) and correct answer. Format each as Question: ... Options: ... Answer: ..."
|
| 27 |
+
response = openai.Completion.create(
|
| 28 |
+
engine="text-davinci-003",
|
| 29 |
+
prompt=prompt,
|
| 30 |
+
max_tokens=500,
|
| 31 |
+
temperature=0.7
|
| 32 |
+
)
|
| 33 |
+
text = response.choices[0].text.strip().split("\n")
|
| 34 |
|
| 35 |
+
html = "<div style='font-family:Poppins,sans-serif;color:#eee;'>"
|
| 36 |
+
for i, line in enumerate(text):
|
| 37 |
+
if line.lower().startswith("question:"):
|
| 38 |
+
html += f"<div style='margin:15px;padding:15px;border-radius:10px;background:#222;box-shadow:0 3px 10px rgba(0,0,0,0.3);'>"
|
| 39 |
+
html += f"<b style='color:#7c3aed;'>{i+1}. {line[9:].strip()}</b><br>"
|
| 40 |
+
elif line.lower().startswith("options:"):
|
| 41 |
+
options = line[8:].split(",")
|
| 42 |
+
for opt in options:
|
| 43 |
+
html += f"<label style='display:block;margin:5px 0;'><input type='checkbox' style='accent-color:#7c3aed;'> {opt.strip()}</label>"
|
| 44 |
+
elif line.lower().startswith("answer:"):
|
| 45 |
+
html += f"<p style='color:#22c55e;display:none;' id='ans{i}'><b>β
Correct Answer:</b> {line[7:].strip()}</p>"
|
| 46 |
+
html += "</div>"
|
| 47 |
+
html += "<button onclick=\"document.querySelectorAll('[id^=ans]').forEach(a=>a.style.display='block');\" style='margin:10px;padding:10px 20px;border:none;border-radius:8px;background:#7c3aed;color:white;cursor:pointer;'>Show Answers</button></div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return html
|
| 49 |
|
| 50 |
+
def study_adviser(topic):
|
| 51 |
+
"""AI gives study advice"""
|
| 52 |
+
prompt = f"You are EduAI. Give 5 practical, concise study tips for the topic: {topic}"
|
| 53 |
+
response = openai.Completion.create(
|
| 54 |
+
engine="text-davinci-003",
|
| 55 |
+
prompt=prompt,
|
| 56 |
+
max_tokens=150,
|
| 57 |
+
temperature=0.7
|
| 58 |
+
)
|
| 59 |
+
return response.choices[0].text.strip().replace("\n", "<br>")
|
| 60 |
+
|
| 61 |
+
def study_planner(task):
|
| 62 |
+
"""AI helps plan study schedule"""
|
| 63 |
+
prompt = f"You are EduAI. Create a 1-day study plan for: {task}. Suggest timing and order of activities."
|
| 64 |
+
response = openai.Completion.create(
|
| 65 |
+
engine="text-davinci-003",
|
| 66 |
+
prompt=prompt,
|
| 67 |
+
max_tokens=150,
|
| 68 |
+
temperature=0.7
|
| 69 |
+
)
|
| 70 |
+
return response.choices[0].text.strip().replace("\n", "<br>")
|
| 71 |
+
|
| 72 |
+
# -------------------- Gradio Interface --------------------
|
| 73 |
+
|
| 74 |
+
with gr.Blocks(css="""
|
| 75 |
+
.gradio-container {background-color:#121212;color:#eee;font-family:Poppins,sans-serif;}
|
| 76 |
+
button {font-weight:600;border-radius:8px;}
|
| 77 |
+
button.primary {background:#7c3aed;color:white;border:none;}
|
| 78 |
+
button.primary:hover {background:#9333ea;}
|
| 79 |
+
h1 {color:#7c3aed;text-align:center;}
|
| 80 |
+
""") as demo:
|
| 81 |
+
|
| 82 |
+
gr.Markdown("<h1>π EduAI β Smart Study Assistant</h1>")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column(scale=1, min_width=250):
|
| 86 |
+
gr.Markdown("### π§ Main Menu")
|
| 87 |
+
|
| 88 |
+
with gr.Accordion("π Study Tutor", open=True):
|
| 89 |
+
chat_history = gr.Chatbot(label="EduAI Tutor", height=350)
|
| 90 |
+
user_msg = gr.Textbox(label="Ask EduAI", placeholder="Type your question...")
|
| 91 |
+
send_btn = gr.Button("Send", elem_classes="primary")
|
| 92 |
+
|
| 93 |
+
with gr.Accordion("π― MCQ Generator", open=False):
|
| 94 |
+
topic_mcq = gr.Textbox(label="Topic for MCQs")
|
| 95 |
+
num_q = gr.Slider(1, 10, value=3, step=1, label="Number of Questions")
|
| 96 |
+
mcq_btn = gr.Button("Generate MCQs", elem_classes="primary")
|
| 97 |
+
mcq_output = gr.HTML("<i>MCQs will appear here...</i>")
|
| 98 |
+
|
| 99 |
+
with gr.Accordion("π‘ Study Adviser", open=False):
|
| 100 |
+
topic_advice = gr.Textbox(label="Topic for advice")
|
| 101 |
+
advice_btn = gr.Button("Get Advice", elem_classes="primary")
|
| 102 |
+
advice_output = gr.HTML("<i>Advice will appear here...</i>")
|
| 103 |
+
|
| 104 |
+
with gr.Accordion("π Study Planner", open=False):
|
| 105 |
+
task_plan = gr.Textbox(label="Task / Subject")
|
| 106 |
+
plan_btn = gr.Button("Generate Plan", elem_classes="primary")
|
| 107 |
+
plan_output = gr.HTML("<i>Plan will appear here...</i>")
|
| 108 |
+
|
| 109 |
+
with gr.Column(scale=3):
|
| 110 |
+
gr.Markdown("<h2 style='color:#7c3aed;'>π Your Study Workspace</h2>")
|
| 111 |
|
| 112 |
+
# -------- Events --------
|
| 113 |
+
send_btn.click(chat_tutor, inputs=[user_msg, chat_history], outputs=[chat_history, chat_history])
|
| 114 |
+
mcq_btn.click(generate_mcqs, inputs=[topic_mcq, num_q], outputs=mcq_output)
|
| 115 |
+
advice_btn.click(study_adviser, inputs=topic_advice, outputs=advice_output)
|
| 116 |
+
plan_btn.click(study_planner, inputs=task_plan, outputs=plan_output)
|
| 117 |
|
| 118 |
demo.launch()
|