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