Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import gradio as gr | |
| from gradio_client import Client | |
| from huggingface_hub import HfApi, hf_hub_download | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ุงูุฅุนุฏุงุฏุงุช ูุงูุฑูุงุจุท ุจูู ุงูู ุณุงุญุงุช | |
| # โ ๏ธ ุชุฃูุฏ ู ู ูุฐู ุงูุฃุณู ุงุก ุจุงูุถุจุท โ ุงุณุชููุชุฌุช ู ู ุฑุณุงูุชูุ ุตุญูุญูุง ุฅุฐุง ุงุฎุชููุช | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| RADAR_SPACE_URL = "Asem75/Aiocr_Radar" | |
| MEDIA_SPACE_URL = "Asem75/My_teacher_enemation" # ู ุณุงุญุฉ ุงูุตูุฑ ูุงูุฃูู ูุดู (SD-Turbo + AnimateDiff) | |
| CONTROLLER_SPACE_URL = "Asem75/My_teacher_controller" # ู ุณุงุญุฉ ุชููุฆุฉ ุงูุฎุฒูุฉ (ููู ุฑุฌุนูุฉ ููุทุ ุงูุชููุฆุฉ ู ูุถู ูููุฉ ููุง ู ุจุงุดุฑุฉ) | |
| VAULT_REPO_ID = "Asem75/aiocr_asistant" | |
| MODEL_ID = "Qwen/Qwen2.5-3B-Instruct" | |
| DEVICE = "cpu" | |
| RADAR_API_KEY = os.environ.get("INTERNAL_API_KEY", "").strip() | |
| HF_TOKEN = os.environ.get("HF_TOKEN", "").strip() | |
| if not HF_TOKEN: | |
| print("โ ๏ธ ุชูุจูู: HF_TOKEN ุบูุฑ ู ุถุจูุท ูู ุฃุณุฑุงุฑ ูุฐู ุงูู ุณุงุญุฉ โ ุงููุฑุงุกุฉ/ุงููุชุงุจุฉ ูู ุงูุฎุฒูุฉ ูุฏ ุชูุดู ุฅุฐุง ูุงูุช ุฎุงุตุฉ.") | |
| _hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else HfApi() | |
| print("โณ ุชุญู ูู ูู ูุฐุฌ ูููู (ุงูุนูู ุงูู ุฏุจุฑ)...") | |
| _tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | |
| _model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, torch_dtype=torch.float32, device_map="cpu", low_cpu_mem_usage=True | |
| ) | |
| print("โ ูููู ุฌุงูุฒ") | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ุชููุฆุฉ ู ููุงุช ุงูุฎุฒูุฉ โ ููุณ ุจููุฉ ููุฏ "controller" ุงูุฐู ุฃุฑุณูุชูุ ู ูุถู ูููุฉ | |
| # ููุง ู ุจุงุดุฑุฉ (ูุง ุชุนุชู ุฏ ุนูู ุงุณุชุฏุนุงุก ู ุณุงุญุฉ ูุฏ ุชููู ูุงุฆู ุฉ ุนูุฏ ูู ุฅููุงุน). | |
| # | |
| # ๐ ๏ธ ุงูุฅุตูุงุญ ุงูุฐู ูุฌุฏุชู ูู ู ุฑุงุฌุนุชู: ุฑูุน ุงูู ูู ุงูุจุฏูู (ุนูุฏ ุนุฏู ูุฌูุฏู) | |
| # ูู ููู ู ุญู ูุงู ุจู try/except ุฎุงุต ุจู โ ุฃู ูุดู ูู ุงูุฑูุน (ุชููู ุฎุงุทุฆ/ุตูุงุญูุงุช) | |
| # ูุงู ุณูุชุณุจุจ ุจุงูููุงุฑ ุงูู ุณุงุญุฉ ุจุงููุงู ู ุนูุฏ ุงูุฅููุงุน. ุงูุขู ู ุญู ู ูููุณุฌููู | |
| # ุงููุดู ูู ุงูุณุฌู ุจุฏู ุฅุณูุงุท ุงูุชุทุจูู. | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| VAULT_STRUCTURE = { | |
| "curriculum_plans/lessons_division.json": { | |
| "description": "ุชูุณูู ุงูุฏุฑูุณ ูุงูู ูุงุฏ ุงูุชุนููู ูุฉ ุจูู ุงูุนูู ู ูุงูุฃุฏุจู ูุงููุณูู ", | |
| "tracks": {"scientific": [], "literary": []} | |
| }, | |
| "curriculum_plans/co_planning_qwen.json": { | |
| "description": "ู ูู ุงูุชุฎุทูุท ุงูู ุดุชุฑู ู ุน ู ูุฏูู ูููู ูุชุญุฏูุฏ ุงูุฃูุฏุงู ุงูุชุฑุจููุฉ", | |
| "qwen_guidelines": {}, "shared_plans": [] | |
| }, | |
| "learning_gaps/student_levels.json": { | |
| "description": "ุณุฌู ู ุณุชููุงุช ุงูุทูุงุจ ูุชุตููู ู ูุงุฑุงุชูู ุงูุชุนููู ูุฉ", | |
| "levels": {"beginner": [], "intermediate": [], "advanced": []} | |
| }, | |
| "learning_gaps/gap_analysis.json": { | |
| "description": "ุชุญููู ุงููุฌูุฉ ุงูุชุนููู ูุฉ ูุฎุทุท ุงูู ุนุงูุฌุฉ ุงููุฑุฏูุฉ ููุทูุงุจ", | |
| "detected_gaps": [], "remedial_actions": [] | |
| }, | |
| "teacher_student_interaction/interaction_logs.json": { | |
| "description": "ุณุฌูุงุช ุนูุงูุฉ ุงูุทุงูุจ ุจุงูุฃุณุชุงุฐ ูุงูุฏุนู ุงูุชูุฌููู", | |
| "teacher_notes": [], "student_feedback": [] | |
| }, | |
| } | |
| def initialize_vault_files(): | |
| log_lines = [] | |
| for file_path, default_content in VAULT_STRUCTURE.items(): | |
| try: | |
| hf_hub_download(repo_id=VAULT_REPO_ID, filename=file_path, repo_type="dataset", token=HF_TOKEN or None) | |
| log_lines.append(f"โ {file_path} ู ูุฌูุฏ ู ุณุจูุงู ูุฌุงูุฒ.") | |
| except Exception: | |
| try: | |
| local_filename = f"/tmp/{os.path.basename(file_path)}" | |
| with open(local_filename, "w", encoding="utf-8") as f: | |
| json.dump(default_content, f, ensure_ascii=False, indent=4) | |
| _hf_api.upload_file( | |
| path_or_fileobj=local_filename, path_in_repo=file_path, | |
| repo_id=VAULT_REPO_ID, repo_type="dataset", token=HF_TOKEN or None, | |
| ) | |
| os.remove(local_filename) | |
| log_lines.append(f"๐ ุชู ุฅูุดุงุก ูุฑูุน: {file_path}") | |
| except Exception as e_upload: | |
| log_lines.append(f"โ ูุดู ุฅูุดุงุก {file_path}: {e_upload}") | |
| return "\n".join(log_lines) | |
| print("โณ ูุญุต ูุชููุฆุฉ ู ููุงุช ุงูุฎุฒูุฉ...") | |
| VAULT_INIT_LOG = initialize_vault_files() | |
| print(VAULT_INIT_LOG) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ุงูู ุนูู ูู ูุงูุฃุตูุงุช (Tab 1 โ ุญุฌุฒ ู ุจุฏุฆูุ ุชุทููุฑ ูุงู ู ูุงุญูุงู) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| TEACHERS = [ | |
| ("ุงูุฃุณุชุงุฐ ุนุจูุฏ", "ar-JO-TaimNeural"), | |
| ("ุงูุฃุณุชุงุฐุฉ ุณูู ู", "ar-EG-SalmaNeural"), | |
| ("ุงูุฃุณุชุงุฐ ุญุงู ุฏ", "ar-SA-HamedNeural"), | |
| ] | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ุงูุฑุจุท ุจู ุณุงุญุฉ ุงูุฑุงุฏุงุฑ (ุตูุช) โ ุจููุณ ูู ุท ุงูุญู ุงูุฉ ุจู ูุชุงุญ ุณุฑู ุงูู ูุนุชู ุฏ ุณุงุจูุงู | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def synth_via_radar(text, speaker_name): | |
| try: | |
| client = Client(RADAR_SPACE_URL) | |
| audio_url = client.predict(text, speaker_name, RADAR_API_KEY, api_name="/synth_arabic") | |
| if audio_url and isinstance(audio_url, (list, tuple)): | |
| audio_url = audio_url[0] | |
| return str(audio_url).strip() if audio_url else None | |
| except Exception as e: | |
| print(f"โ ๏ธ ุฎุทุฃ ุงูุฑุงุฏุงุฑ: {e}") | |
| return None | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ุงูุฑุจุท ุจู ุณุงุญุฉ ุงููุณุงุฆุท (ุตูุฑ/ุฃูู ูุดู) | |
| # โ ๏ธ ู ูุงุญุธุฉ ุฃู ุงู: ููุทุชุง API ูุฐู (generate_image, generate_animation) ูู | |
| # ู ุณุงุญุฉ ุงููุณุงุฆุท ุบูุฑ ู ุญู ูุชูู ุจุฃู ู ูุชุงุญ ุณุฑู ุญุงููุงู (ุจุฎูุงู ุงูุฑุงุฏุงุฑ). ุฅุฐุง | |
| # ุฃุฑุฏุช ุญู ุงูุชูู ุง ุจููุณ ู ุจุฏุฃ INTERNAL_API_KEYุ ุฃุฎุจุฑูู ูุฃุถููู ููุงู ุฃูุถุงู. | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def generate_image_via_media(prompt): | |
| try: | |
| client = Client(MEDIA_SPACE_URL) | |
| return client.predict(prompt, api_name="/generate_image") | |
| except Exception as e: | |
| print(f"โ ๏ธ ุฎุทุฃ ู ุณุงุญุฉ ุงููุณุงุฆุท (ุตูุฑุฉ): {e}") | |
| return None | |
| def generate_animation_via_media(prompt): | |
| try: | |
| client = Client(MEDIA_SPACE_URL) | |
| return client.predict(prompt, api_name="/generate_animation") | |
| except Exception as e: | |
| print(f"โ ๏ธ ุฎุทุฃ ู ุณุงุญุฉ ุงููุณุงุฆุท (ุฃูู ูุดู): {e}") | |
| return None | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ุงูุนูู ุงูู ุฏุจุฑ (ูููู) | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| def ask_teacher(prompt, history): | |
| history = history or [] | |
| messages = [{ | |
| "role": "system", | |
| "content": "ุฃูุช ููููุ ุงูุนูู ุงูู ุฏุจุฑ ูู ูุตุฉ ุชุนููู ูุฉ ุนุฑุจูุฉ. ุชุณุงุนุฏ ูู ุตูุงุบุฉ ุฏุฑูุณ ูู ุญุชูู ุชุนููู ู ูุตูุฑ ููุงุถุญ ุจุงูุนุฑุจูุฉุ ูุชุฌูุจ ุจุฅูุฌุงุฒ ููุถูุญ." | |
| }] | |
| messages.extend(history) | |
| messages.append({"role": "user", "content": prompt}) | |
| text_prompt = _tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| inputs = _tokenizer([text_prompt], return_tensors="pt").to(DEVICE) | |
| with torch.no_grad(): | |
| out_ids = _model.generate( | |
| **inputs, max_new_tokens=200, temperature=0.5, do_sample=True, | |
| repetition_penalty=1.15, pad_token_id=_tokenizer.eos_token_id | |
| ) | |
| out_ids = [o[len(i):] for i, o in zip(inputs.input_ids, out_ids)] | |
| response = _tokenizer.batch_decode(out_ids, skip_special_tokens=True)[0].strip() | |
| new_history = history + [{"role": "user", "content": prompt}, {"role": "assistant", "content": response}] | |
| return response, new_history | |
| def integration_test(prompt, teacher_choice, gen_image, gen_anim, history_state): | |
| if not prompt or not prompt.strip(): | |
| return "โ ๏ธ ุงูุชุจ ูุตุงู ุฃููุงู", history_state, None, None, None | |
| response, new_history = ask_teacher(prompt, history_state) | |
| speaker_name = dict(TEACHERS).get(teacher_choice, "ar-JO-TaimNeural") | |
| audio_path = synth_via_radar(response, speaker_name) | |
| image_path = generate_image_via_media(response) if gen_image else None | |
| anim_path = generate_animation_via_media(response) if gen_anim else None | |
| return response, new_history, audio_path, image_path, anim_path | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| # ุงููุงุฌูุฉ: ุณุชุฉ ุชุจููุจุงุช ุญุฌุฒ ู ูุงู + ููุญุฉ ูุญุต ุงูุฑุจุท | |
| # โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| with gr.Blocks(title="My Teacher โ ุงูู ูุตุฉ ุงูุฑุฆูุณูุฉ", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# ๐ ุงูู ูุตุฉ ุงูุชุนููู ูุฉ ุงูุฐููุฉ ุงูู ูุญุฏุฉ") | |
| qwen_history_state = gr.State([]) | |
| with gr.Tabs(): | |
| with gr.Tab("๐จโ๐ซ ุฅุนุฏุงุฏุงุช ุงูู ุนูู ูุงูุตูุช"): | |
| gr.Markdown( | |
| "๐ง **ููุฏ ุงูุชุทููุฑ.** ุณูุชู ููุง: ุชุตูุญ ุงูู ุนูู ูู ุงูู ุชุงุญููุ ูุงุฎุชูุงุฑ " | |
| "ุงูุฃุณุชุงุฐ ุงูู ูุงุณุจุ ูุฑุจุท ูู ู ุนูู ุจุฎุงู ุฉ ุตูุชูุฉ ุนุจุฑ edge-tts/ุงูุฑุงุฏุงุฑ." | |
| ) | |
| with gr.Tab("๐ฌ ุงูููุฏูููุงุช ุงูุชุนููู ูุฉ ุงูู ุณุฌูุฉ"): | |
| gr.Markdown("๐ง **ููุฏ ุงูุชุทููุฑ.** ู ูุชุจุฉ ุงูุฏุฑูุณ ูุงูููุฏูููุงุช ุงูู ุณุฌูุฉ ู ุณุจูุงู ููู ุฑุงุฌุนุฉ.") | |
| with gr.Tab("๐๏ธ ุฎุทุท ุงูุฏุฑูุณ ูุงูุชูุณูู "): | |
| gr.Markdown( | |
| "๐ง **ููุฏ ุงูุชุทููุฑ.** ูุงูุฐุฉ ุชูุงุนููุฉ ู ุน ูููู ููุชุฎุทูุท ุงูู ุดุชุฑูุ " | |
| "ูุชูุณูู ุงูู ูุงุฏ ุจูู ุงูุนูู ู ูุงูุฃุฏุจู." | |
| ) | |
| with gr.Tab("๐ ุงูู ุณุชููุงุช ูุชุญููู ุงููุฌูุฉ ุงูุชุนููู ูุฉ"): | |
| gr.Markdown( | |
| "๐ง **ููุฏ ุงูุชุทููุฑ.** ุชุชุจุน ู ุณุชูู ุงูุทุงูุจ ูุชุญููู ุงูุฃุฎุทุงุก " | |
| "(ุจุงูุงุณุชุนุงูุฉ ุจู CAMeL-BERT) ูุฎุทุท ุณุฏ ุงููุฌูุงุช." | |
| ) | |
| with gr.Tab("๐ฌ ุนูุงูุฉ ุงูุทุงูุจ ูุงูุฃุณุชุงุฐ"): | |
| gr.Markdown("๐ง **ููุฏ ุงูุชุทููุฑ.** ุงูุฑุณุงุฆู ุงูุชูุฌูููุฉุ ุงูุชูููู ุงุชุ ูุงูุฏุนู ุงููุฑุฏู.") | |
| with gr.Tab("๐ผ๏ธ ุฃุฑุดูู ูุฅุฏุงุฑุฉ ุงููุณุงุฆุท"): | |
| gr.Markdown("๐ง **ููุฏ ุงูุชุทููุฑ.** ููุฑุณุฉ ุงูุตูุฑ ูุงูุฃูู ูุดู ุงูู ูููุฏุฉ ูู ูุน ุฅุนุงุฏุฉ ุงูุชูููุฏ.") | |
| with gr.Tab("๐ ููุญุฉ ูุญุต ุงูุฑุจุท (API)"): | |
| gr.Markdown( | |
| "ุชุฌุฑุจุฉ ู ุจุงุดุฑุฉ ุชูุซุจุช ุฃู **ูููู + ุงูุฑุงุฏุงุฑ (ุตูุช) + ู ุณุงุญุฉ ุงููุณุงุฆุท (ุตูุฑุฉ/ุฃูู ูุดู)** " | |
| "ู ุฑุจูุทูู ูุนููุงู ุนุจุฑ APIุ ูุจู ุจูุงุก ุงูุชุจููุจุงุช ุงูุณุช ุจุงูุชูุตูู." | |
| ) | |
| prompt_in = gr.Textbox(label="โ๏ธ ุงูุชุจ ููุฑุฉ ุฃู ุณุคุงูุงู ุชุนููู ูุงู", lines=3) | |
| teacher_pick = gr.Dropdown(choices=[t[0] for t in TEACHERS], value=TEACHERS[0][0], label="๐๏ธ ุงุฎุชุฑ ุงูู ุนูู /ุงูุตูุช") | |
| with gr.Row(): | |
| want_image = gr.Checkbox(label="๐ผ๏ธ ุชูููุฏ ุตูุฑุฉ ุชูุถูุญูุฉ", value=False) | |
| want_anim = gr.Checkbox(label="๐ฌ ุชูููุฏ ุฃูู ูุดู (ุฃุจุทุฃ ุจูุซูุฑ)", value=False) | |
| run_btn = gr.Button("๐ ุฌุฑูุจ ุงูุฑุจุท ุงููุงู ู", variant="primary") | |
| response_out = gr.Textbox(label="๐ ุฑุฏ ูููู", lines=4, interactive=False) | |
| audio_out = gr.Audio(label="๐ ุงูุตูุช (ุนุจุฑ ุงูุฑุงุฏุงุฑ)", type="filepath", autoplay=True) | |
| with gr.Row(): | |
| image_out = gr.Image(label="๐ผ๏ธ ุงูุตูุฑุฉ (ุนุจุฑ ู ุณุงุญุฉ ุงููุณุงุฆุท)") | |
| anim_out = gr.Image(label="๐ฌ ุงูุฃูู ูุดู GIF (ุนุจุฑ ู ุณุงุญุฉ ุงููุณุงุฆุท)") | |
| run_btn.click( | |
| fn=integration_test, | |
| inputs=[prompt_in, teacher_pick, want_image, want_anim, qwen_history_state], | |
| outputs=[response_out, qwen_history_state, audio_out, image_out, anim_out], | |
| api_name=False, | |
| ) | |
| with gr.Accordion("๐๏ธ ุณุฌู ุชููุฆุฉ ุงูุฎุฒูุฉ ุนูุฏ ุงูุฅููุงุน", open=False): | |
| gr.Textbox(value=VAULT_INIT_LOG, lines=8, interactive=False, show_label=False) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |