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