Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,144 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
GROQ_API_KEY = "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"
|
| 7 |
-
|
| 8 |
-
GROQ_MODEL = "mixtral-8x7b-32768"
|
| 9 |
|
| 10 |
-
def
|
| 11 |
headers = {
|
| 12 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 13 |
"Content-Type": "application/json"
|
| 14 |
}
|
| 15 |
-
|
| 16 |
data = {
|
| 17 |
"model": GROQ_MODEL,
|
| 18 |
-
"messages": [{"role": "user", "content": prompt}]
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
-
|
| 22 |
-
response = requests.post(GROQ_API_URL, headers=headers, json=data)
|
| 23 |
-
|
| 24 |
-
if response.status_code == 200:
|
| 25 |
-
return response.json()["choices"][0]["message"]["content"]
|
| 26 |
-
else:
|
| 27 |
-
return f"❌ Error: {response.status_code} - {response.text}"
|
| 28 |
-
|
| 29 |
-
# ========== CLASS, SUBJECT, CHAPTER OPTIONS ==========
|
| 30 |
-
|
| 31 |
-
subject_map = {
|
| 32 |
-
"9th": ["Physics", "Chemistry", "Biology"],
|
| 33 |
-
"10th": ["Physics", "Chemistry", "Biology"],
|
| 34 |
-
"1st Year": ["Physics", "Chemistry", "Biology"],
|
| 35 |
-
"2nd Year": ["Physics", "Chemistry", "Biology"]
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
chapter_map = {
|
| 39 |
-
"Physics": [
|
| 40 |
-
"Physical Quantities and Units", "Kinematics", "Dynamics", "Turning Effect of Forces",
|
| 41 |
-
"Gravitation", "Work and Energy", "Properties of Matter", "Thermal Properties of Matter",
|
| 42 |
-
"Transfer of Heat", "Simple Harmonic Motion", "Sound", "Geometrical Optics",
|
| 43 |
-
"Electrostatics", "Current Electricity", "Electromagnetism", "Electronics", "Dawn of Modern Physics"
|
| 44 |
-
],
|
| 45 |
-
"Chemistry": [
|
| 46 |
-
"Fundamentals of Chemistry", "Structure of Atom", "Periodic Table", "Chemical Bonding",
|
| 47 |
-
"States of Matter", "Solutions", "Electrochemistry", "Chemical Kinetics", "Organic Chemistry", "Environmental Chemistry"
|
| 48 |
-
],
|
| 49 |
-
"Biology": [
|
| 50 |
-
"Introduction to Biology", "Biological Molecules", "Cell Biology", "Enzymes",
|
| 51 |
-
"Bioenergetics", "Nutrition", "Transport", "Respiration", "Coordination", "Homeostasis"
|
| 52 |
-
]
|
| 53 |
}
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def update_subjects(selected_class):
|
| 60 |
-
return gr.Dropdown.update(choices=subject_map.get(selected_class, []), value=None)
|
| 61 |
-
|
| 62 |
-
def update_chapters(selected_subject):
|
| 63 |
-
return gr.Dropdown.update(choices=chapter_map.get(selected_subject, []), value=None)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
You are an AI Textbook Tutor helping a student of Class {selected_class}.
|
| 71 |
-
Subject: {selected_subject}
|
| 72 |
-
Chapter: {selected_chapter}
|
| 73 |
-
Task: {selected_task}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
""
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
with gr.Blocks() as demo:
|
| 89 |
-
gr.Markdown("# 📚 AI Textbook Tutor\nSelect your class, subject, and chapter to get help!")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
chapter_choice = gr.Dropdown(label="Select Chapter")
|
| 94 |
-
task_choice = gr.Radio(task_options, label="Select Task")
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
run_button.click(
|
| 103 |
-
|
| 104 |
-
inputs=[
|
| 105 |
outputs=output_box
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# ------------------ GROQ SETTINGS ------------------
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "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"
|
| 7 |
+
GROQ_MODEL = "llama3-70b-8192"
|
|
|
|
| 8 |
|
| 9 |
+
def query_gpt(prompt):
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 12 |
"Content-Type": "application/json"
|
| 13 |
}
|
|
|
|
| 14 |
data = {
|
| 15 |
"model": GROQ_MODEL,
|
| 16 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 17 |
+
}
|
| 18 |
+
response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=data, headers=headers)
|
| 19 |
+
return response.json()['choices'][0]['message']['content']
|
| 20 |
+
|
| 21 |
+
# ------------------ SYLLABUS DATA ------------------
|
| 22 |
+
class_subject_chapters = {
|
| 23 |
+
"9": {
|
| 24 |
+
"Physics": [
|
| 25 |
+
"Introduction to Physics", "Kinematics", "Dynamics", "Turning Effect of Forces",
|
| 26 |
+
"Gravitation", "Work and Energy", "Properties of Matter", "Thermal Properties of Matter",
|
| 27 |
+
"Transfer of Heat"
|
| 28 |
+
],
|
| 29 |
+
"Biology": [
|
| 30 |
+
"Introduction to Biology", "Solving a Biological Problem", "Biodiversity",
|
| 31 |
+
"Cells and Tissues", "Cell Cycle", "Enzymes", "Bioenergetics", "Nutrition",
|
| 32 |
+
"Transport"
|
| 33 |
+
],
|
| 34 |
+
"Chemistry": [
|
| 35 |
+
"Fundamentals of Chemistry", "Structure of Atoms", "Periodic Table",
|
| 36 |
+
"Structure of Molecules", "Physical States of Matter", "Solutions",
|
| 37 |
+
"Electrochemistry", "Chemical Reactivity"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
"10": {
|
| 41 |
+
"Physics": [
|
| 42 |
+
"Simple Harmonic Motion and Waves", "Sound", "Geometrical Optics",
|
| 43 |
+
"Electrostatics", "Current Electricity", "Electromagnetism",
|
| 44 |
+
"Basic Electronics", "Information and Communication Technology",
|
| 45 |
+
"Atomic and Nuclear Physics"
|
| 46 |
+
],
|
| 47 |
+
"Biology": [
|
| 48 |
+
"Gaseous Exchange", "Homeostasis", "Coordination and Control",
|
| 49 |
+
"Support and Movement", "Reproduction", "Inheritance",
|
| 50 |
+
"Man and His Environment", "Biotechnology", "Pharmacology"
|
| 51 |
+
],
|
| 52 |
+
"Chemistry": [
|
| 53 |
+
"Chemical Equilibrium", "Acids, Bases and Salts", "Organic Chemistry",
|
| 54 |
+
"Hydrocarbons", "Biochemistry", "Environmental Chemistry", "Chemical Industries"
|
| 55 |
+
]
|
| 56 |
+
},
|
| 57 |
+
"11": {
|
| 58 |
+
"Physics": [
|
| 59 |
+
"Measurements", "Vectors and Equilibrium", "Motion and Force",
|
| 60 |
+
"Work and Energy", "Circular Motion", "Fluid Dynamics",
|
| 61 |
+
"Oscillations", "Waves", "Physical Optics", "Thermodynamics"
|
| 62 |
+
],
|
| 63 |
+
"Biology": [
|
| 64 |
+
"Introduction to Biology", "Biological Molecules", "Enzymes",
|
| 65 |
+
"The Cell", "Cell Cycle", "Variety of Life", "Kingdom Prokaryotae",
|
| 66 |
+
"Kingdom Protista", "Kingdom Fungi", "Kingdom Plantae", "Kingdom Animalia"
|
| 67 |
+
],
|
| 68 |
+
"Chemistry": [
|
| 69 |
+
"Basic Concepts", "Experimental Techniques", "Gases", "Liquids and Solids",
|
| 70 |
+
"Atomic Structure", "Chemical Bonding", "Thermochemistry",
|
| 71 |
+
"Chemical Equilibrium", "Ionic Equilibrium", "Redox Reactions"
|
| 72 |
+
]
|
| 73 |
+
},
|
| 74 |
+
"12": {
|
| 75 |
+
"Physics": [
|
| 76 |
+
"Electrostatics", "Current Electricity", "Electromagnetism", "Electromagnetic Induction",
|
| 77 |
+
"Alternating Current", "Physics of Solids", "Electronics", "Dawn of Modern Physics",
|
| 78 |
+
"Atomic Spectra", "Nuclear Physics"
|
| 79 |
+
],
|
| 80 |
+
"Biology": [
|
| 81 |
+
"Homeostasis", "Support and Movement", "Coordination and Control",
|
| 82 |
+
"Reproduction", "Growth and Development", "Chromosomes and DNA",
|
| 83 |
+
"Biotechnology", "Evolution", "Ecosystem", "Some Major Ecosystems"
|
| 84 |
+
],
|
| 85 |
+
"Chemistry": [
|
| 86 |
+
"Periodic Classification", "Transition Elements", "Chemical Reactivity",
|
| 87 |
+
"Alcohols and Phenols", "Aldehydes and Ketones", "Carboxylic Acids",
|
| 88 |
+
"Amines", "Polymers", "Biochemistry", "Environmental Chemistry"
|
| 89 |
+
]
|
| 90 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
+
# ------------------ MAIN BOT FUNCTION ------------------
|
| 94 |
+
def tutor_bot(selected_class, selected_subject, selected_chapter, action_type):
|
| 95 |
+
prompt = f"The user is studying in Class {selected_class}. Give a detailed response for the chapter '{selected_chapter}' in the subject '{selected_subject}'."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
if action_type == "Summarize Important Points":
|
| 98 |
+
prompt = f"Summarize the key points of the chapter '{selected_chapter}' from Class {selected_class} {selected_subject}."
|
| 99 |
+
elif action_type == "Generate MCQs":
|
| 100 |
+
prompt = f"Create 5 multiple choice questions (MCQs) with 4 options each for the chapter '{selected_chapter}' from Class {selected_class} {selected_subject}."
|
| 101 |
+
elif action_type == "Simplify Concepts":
|
| 102 |
+
prompt = f"Explain in very simple terms the important concepts from the chapter '{selected_chapter}' from Class {selected_class} {selected_subject} for a 14-year-old student."
|
| 103 |
|
| 104 |
+
gpt_response = query_gpt(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
# Add YouTube search link
|
| 107 |
+
search_query = f"{selected_class} {selected_subject} {selected_chapter} in Urdu"
|
| 108 |
+
youtube_link = f"https://www.youtube.com/results?search_query={search_query.replace(' ', '+')}"
|
| 109 |
+
gpt_response += f"\n\n🎥 **Watch this topic on YouTube:** [Click here]({youtube_link})"
|
| 110 |
|
| 111 |
+
return gpt_response
|
| 112 |
|
| 113 |
+
# ------------------ GRADIO UI ------------------
|
| 114 |
+
with gr.Blocks(title="AI Textbook Tutor") as app:
|
| 115 |
+
gr.Markdown("## 📘 AI Textbook Tutor\nSelect your class, subject, and chapter to get summaries, MCQs, or simplified explanations!")
|
| 116 |
|
| 117 |
+
class_dropdown = gr.Dropdown(label="Select Class", choices=list(class_subject_chapters.keys()))
|
| 118 |
+
subject_dropdown = gr.Dropdown(label="Select Subject")
|
| 119 |
+
chapter_dropdown = gr.Dropdown(label="Select Chapter")
|
| 120 |
+
action_dropdown = gr.Radio(["Summarize Important Points", "Generate MCQs", "Simplify Concepts"], label="Select Task")
|
| 121 |
|
| 122 |
+
run_button = gr.Button("Run Tutor 🧠")
|
| 123 |
+
output_box = gr.Markdown() # Changed from Textbox to Markdown for clickable links
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
def update_subjects(selected_class):
|
| 126 |
+
return gr.update(choices=list(class_subject_chapters[selected_class].keys()), value=None)
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
def update_chapters(selected_class, selected_subject):
|
| 129 |
+
return gr.update(choices=class_subject_chapters[selected_class][selected_subject], value=None)
|
| 130 |
|
| 131 |
+
class_dropdown.change(update_subjects, inputs=class_dropdown, outputs=subject_dropdown)
|
| 132 |
+
subject_dropdown.change(update_chapters, inputs=[class_dropdown, subject_dropdown], outputs=chapter_dropdown)
|
| 133 |
|
| 134 |
run_button.click(
|
| 135 |
+
tutor_bot,
|
| 136 |
+
inputs=[class_dropdown, subject_dropdown, chapter_dropdown, action_dropdown],
|
| 137 |
outputs=output_box
|
| 138 |
)
|
| 139 |
|
| 140 |
+
app.launch()
|
| 141 |
+
|
| 142 |
|
| 143 |
|
| 144 |
|