Spaces:
Sleeping
Sleeping
File size: 15,095 Bytes
b9eb636 fd1afcd b9eb636 fd1afcd b9eb636 dee1798 b9eb636 dee1798 fd1afcd dee1798 fd1afcd b9eb636 fd1afcd dee1798 fd1afcd b9eb636 dee1798 86a5e03 b9eb636 dee1798 b9eb636 dee1798 b9eb636 dee1798 fd1afcd dee1798 fd1afcd dee1798 fd1afcd dee1798 fd1afcd dee1798 fd1afcd dee1798 fd1afcd dee1798 fd1afcd dee1798 b9eb636 dee1798 b9eb636 dee1798 b9eb636 dee1798 b9eb636 dee1798 b9eb636 dee1798 b9eb636 dee1798 b9eb636 dee1798 86a5e03 dee1798 b9eb636 dee1798 b9eb636 dee1798 b9eb636 dee1798 fd1afcd dee1798 86a5e03 dee1798 fd1afcd dee1798 fd1afcd dee1798 fd1afcd dee1798 b9eb636 | 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 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)
|