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()