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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Initialize Groq API client
|
| 6 |
+
api_key = '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'
|
| 7 |
+
client = Groq(api_key=api_key)
|
| 8 |
+
|
| 9 |
+
# Lessons structure
|
| 10 |
+
lessons = {
|
| 11 |
+
"beginner": {
|
| 12 |
+
"topics": [
|
| 13 |
+
"Introduction to Arabic Letters",
|
| 14 |
+
"Basic Arabic Grammar: Nouns and Pronouns",
|
| 15 |
+
"Basic Arabic Sentence Structure"
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
"intermediate": {
|
| 19 |
+
"topics": [
|
| 20 |
+
"Verb Forms in Arabic",
|
| 21 |
+
"Arabic Tenses and Conjugation",
|
| 22 |
+
"Arabic Question Structures"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
"advanced": {
|
| 26 |
+
"topics": [
|
| 27 |
+
"Advanced Arabic Sentence Structure",
|
| 28 |
+
"Arabic Vocabulary Building",
|
| 29 |
+
"Arabic Syntax and Semantics"
|
| 30 |
+
]
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
def save_user_data(user_id, data):
|
| 35 |
+
with open(f"{user_id}_data.json", "w") as file:
|
| 36 |
+
json.dump(data, file)
|
| 37 |
+
|
| 38 |
+
def load_user_data(user_id):
|
| 39 |
+
try:
|
| 40 |
+
with open(f"{user_id}_data.json", "r") as file:
|
| 41 |
+
return json.load(file)
|
| 42 |
+
except FileNotFoundError:
|
| 43 |
+
return {"level": None, "current_lesson": 0, "score": 0}
|
| 44 |
+
|
| 45 |
+
def generate_response(level, topic):
|
| 46 |
+
prompt = f"Generate a lesson on {topic} for {level} level Arabic learner."
|
| 47 |
+
response = client.chat.completions.create(
|
| 48 |
+
messages=[{"role": "user", "content": prompt}],
|
| 49 |
+
model="llama-3.3-70b-versatile",
|
| 50 |
+
)
|
| 51 |
+
return response.choices[0].message.content
|
| 52 |
+
|
| 53 |
+
def create_interface():
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("(Smart AI) Smart Arabic-Instructor: Chatbot for Quranic Arabic Learning")
|
| 56 |
+
|
| 57 |
+
# User ID input
|
| 58 |
+
user_id = gr.Textbox(label="Enter User ID", placeholder="Type your User ID")
|
| 59 |
+
|
| 60 |
+
# Action selection
|
| 61 |
+
action = gr.Radio(
|
| 62 |
+
choices=["start", "select_level", "start_learning"],
|
| 63 |
+
label="Choose Action",
|
| 64 |
+
value="start",
|
| 65 |
+
visible=False
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Welcome message
|
| 69 |
+
welcome_msg = gr.Textbox(
|
| 70 |
+
label="Welcome",
|
| 71 |
+
visible=False,
|
| 72 |
+
interactive=False
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Level selection
|
| 76 |
+
level_select = gr.Dropdown(
|
| 77 |
+
choices=["beginner", "intermediate", "advanced"],
|
| 78 |
+
label="Select Level",
|
| 79 |
+
visible=False
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Topic selection
|
| 83 |
+
topic_select = gr.Dropdown(
|
| 84 |
+
choices=[],
|
| 85 |
+
label="Select Topic",
|
| 86 |
+
visible=False
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Output box for responses
|
| 90 |
+
output_box = gr.Textbox(
|
| 91 |
+
label="Lesson Content",
|
| 92 |
+
interactive=False,
|
| 93 |
+
visible=False,
|
| 94 |
+
lines=10
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Submit button
|
| 98 |
+
submit_btn = gr.Button("Submit", visible=False)
|
| 99 |
+
|
| 100 |
+
def show_welcome(user_id):
|
| 101 |
+
if not user_id:
|
| 102 |
+
return {
|
| 103 |
+
welcome_msg: gr.update(visible=False),
|
| 104 |
+
action: gr.update(visible=False),
|
| 105 |
+
submit_btn: gr.update(visible=False)
|
| 106 |
+
}
|
| 107 |
+
return {
|
| 108 |
+
welcome_msg: gr.update(visible=True, value=f"Welcome {user_id}! Please select an action to continue."),
|
| 109 |
+
action: gr.update(visible=True),
|
| 110 |
+
submit_btn: gr.update(visible=True)
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
def update_interface(user_id, action_selected):
|
| 114 |
+
if not user_id:
|
| 115 |
+
return {
|
| 116 |
+
level_select: gr.update(visible=False),
|
| 117 |
+
topic_select: gr.update(visible=False),
|
| 118 |
+
output_box: gr.update(visible=False),
|
| 119 |
+
welcome_msg: gr.update(value="Please enter a User ID first.", visible=True)
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
if action_selected == "start":
|
| 123 |
+
return {
|
| 124 |
+
level_select: gr.update(visible=False),
|
| 125 |
+
topic_select: gr.update(visible=False),
|
| 126 |
+
output_box: gr.update(visible=True, value="Welcome! Select 'select_level' to choose your level.")
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
elif action_selected == "select_level":
|
| 130 |
+
return {
|
| 131 |
+
level_select: gr.update(visible=True),
|
| 132 |
+
topic_select: gr.update(visible=False),
|
| 133 |
+
output_box: gr.update(visible=True, value="Please select your level.")
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
elif action_selected == "start_learning":
|
| 137 |
+
current_level = load_user_data(user_id).get("level")
|
| 138 |
+
if not current_level:
|
| 139 |
+
return {
|
| 140 |
+
level_select: gr.update(visible=True),
|
| 141 |
+
topic_select: gr.update(visible=True, choices=lessons["beginner"]["topics"]),
|
| 142 |
+
output_box: gr.update(visible=True, value="Please select a level and topic to begin.")
|
| 143 |
+
}
|
| 144 |
+
topics = lessons[current_level]["topics"]
|
| 145 |
+
return {
|
| 146 |
+
level_select: gr.update(visible=True),
|
| 147 |
+
topic_select: gr.update(visible=True, choices=topics),
|
| 148 |
+
output_box: gr.update(visible=True, value="Select a topic to start learning.")
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
def handle_submit(user_id, action_selected, level, topic):
|
| 152 |
+
if not user_id:
|
| 153 |
+
return "Please enter a User ID first."
|
| 154 |
+
|
| 155 |
+
if action_selected == "start":
|
| 156 |
+
return "Please select 'select_level' to choose your level."
|
| 157 |
+
|
| 158 |
+
elif action_selected == "select_level":
|
| 159 |
+
if not level:
|
| 160 |
+
return "Please select a level."
|
| 161 |
+
user_data = load_user_data(user_id)
|
| 162 |
+
user_data["level"] = level
|
| 163 |
+
save_user_data(user_id, user_data)
|
| 164 |
+
return f"Level set to {level}. Select 'start_learning' to begin your lessons."
|
| 165 |
+
|
| 166 |
+
elif action_selected == "start_learning":
|
| 167 |
+
if not level or not topic:
|
| 168 |
+
return "Please select both level and topic."
|
| 169 |
+
lesson_content = generate_response(level, topic)
|
| 170 |
+
return f"Lesson on {topic} for {level} level:\n\n{lesson_content}"
|
| 171 |
+
|
| 172 |
+
# Event handlers
|
| 173 |
+
user_id.change(
|
| 174 |
+
show_welcome,
|
| 175 |
+
inputs=[user_id],
|
| 176 |
+
outputs=[welcome_msg, action, submit_btn]
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
action.change(
|
| 180 |
+
update_interface,
|
| 181 |
+
inputs=[user_id, action],
|
| 182 |
+
outputs=[level_select, topic_select, output_box]
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
level_select.change(
|
| 186 |
+
lambda level: gr.update(choices=lessons[level]["topics"]) if level else gr.update(choices=[]),
|
| 187 |
+
inputs=[level_select],
|
| 188 |
+
outputs=[topic_select]
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
submit_btn.click(
|
| 192 |
+
handle_submit,
|
| 193 |
+
inputs=[user_id, action, level_select, topic_select],
|
| 194 |
+
outputs=[output_box]
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
return demo
|
| 198 |
+
|
| 199 |
+
if __name__ == "__main__":
|
| 200 |
+
demo = create_interface()
|
| 201 |
+
demo.launch()
|