Update app.py
Browse files8 unmodified lines910111213141516171819202122235 unmodified lines29303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941 unmodified line1961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362 unmodified lines23924024124224324424510 unmodified lines2562572582592602612622632642652662672682692702712722732742752762772781 unmodified line2802812822832842852862872882892902912922932942952962972982993003013027 unmodified lines3103113123133143158 unmodified linesfrom transformers import (AutoTokenizer,AutoModelForCausalLM,LlavaNextProcessor,LlavaNextForConditionalGeneration,WhisperProcessor,WhisperForConditionalGeneration,SpeechT5Processor,SpeechT5ForTextToSpeech,SpeechT5HifiGan)from datasets import load_datasetimport osimport scipy.io.wavfile as wavfileimport io5 unmodified linesif token:login(token=token)else:print("Warning: HF_API_TOKEN not set. Some models might require login.")# Device Configurationdevice = "cuda" if torch.cuda.is_available() else "cpu"print(f"Using device: {device}")# 1. Text Generation Model: microsoft/Phi-4-reasoning-plustext_model_name = "microsoft/Phi-4-reasoning-plus"print(f"Loading text model: {text_model_name}")text_tokenizer = AutoTokenizer.from_pretrained(text_model_name)text_model = AutoModelForCausalLM.from_pretrained(text_model_name,torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32, # bfloat16 for better performance if availabledevice_map="auto", # Automatically uses available GPUs or CPUtrust_remote_code=True # Phi models may require this)print("Text model loaded.")# 2. Image Analysis Model: llava-hf/llava-1.5-7b-hfimage_model_name = "llava-hf/llava-1.5-7b-hf"print(f"Loading image model: {image_model_name}")image_processor = LlavaNextProcessor.from_pretrained(image_model_name)image_model = LlavaNextForConditionalGeneration.from_pretrained(image_model_name,torch_dtype=torch.float16 if device == "cuda" else torch.float32,device_map="auto")print("Image model loaded.")# 3. Speech-to-Text Model: openai/whisper-large-v3stt_model_name = "openai/whisper-large-v3"print(f"Loading STT model: {stt_model_name}")stt_processor = WhisperProcessor.from_pretrained(stt_model_name)stt_model = WhisperForConditionalGeneration.from_pretrained(stt_model_name).to(device)stt_model.config.forced_decoder_ids = None # For multilingual capabilitiesprint("STT model loaded.")# 4. Text-to-Speech Model: microsoft/speecht5_tts & microsoft/speecht5_hifigantts_processor_name = "microsoft/speecht5_tts"tts_model_name = "microsoft/speecht5_tts"tts_vocoder_name = "microsoft/speecht5_hifigan"print(f"Loading TTS processor: {tts_processor_name}")tts_processor = SpeechT5Processor.from_pretrained(tts_processor_name)print(f"Loading TTS model: {tts_model_name}")tts_model = SpeechT5ForTextToSpeech.from_pretrained(tts_model_name).to(device)print(f"Loading TTS vocoder: {tts_vocoder_name}")tts_vocoder = SpeechT5HifiGan.from_pretrained(tts_vocoder_name).to(device)# Load speaker embeddings (example from CMU ARCTIC dataset)print("Loading speaker embeddings for TTS...")try:embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")# speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0).to(device) # A specific speaker# Let's pick a female voice, e.g., slt (female)# Find an entry for 'slt' speaker, or use a default if not easily found by index.# For simplicity, using a known good index for a female voice if available, otherwise a default.# This part might need adjustment based on the dataset structure or desired voice.# Example: embeddings_dataset is a list of dicts. Find one with speaker 'slt'.# For now, let's use a fixed index that often corresponds to a clear female voice.speaker_id_index = 7306 # Example index, might need to find a better one or allow selection# A common female speaker from this dataset is 'slt'. Let's try to find her or use a default.# The dataset has 'slt_arctic_a0001' to 'slt_arctic_b0593'. Index 7306 is 'awb_arctic_a0001'.# Let's try to find 'slt' if possible, otherwise use a default. The dataset has 8803 entries.# For simplicity, we'll use a default index. A common one used in examples is 7306.# A known female voice index from this dataset (often 'slt' or similar) is around this range.# Let's use a specific one for 'slt' if we can find it, otherwise a default.# The dataset items are like {'audio': ..., 'filename': 'arctic_a0001.wav', 'speaker': 'slt', 'text': ..., 'xvector': ...}# Let's try to find an 'slt' speaker explicitlyslt_speaker_embedding = Nonefor i in range(len(embeddings_dataset)):if embeddings_dataset[i]['speaker'] == 'slt':slt_speaker_embedding = torch.tensor(embeddings_dataset[i]["xvector"]).unsqueeze(0).to(device)print("Found 'slt' speaker embedding.")breakif slt_speaker_embedding is None:print("Could not find 'slt' speaker, using default index 7306.")slt_speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0).to(device)speaker_embeddings = slt_speaker_embeddingexcept Exception as e:print(f"Could not load speaker embeddings: {e}. TTS might not work optimally or at all.")speaker_embeddings = torch.randn((1, 512)).to(device) # Fallback to random embeddingsprint("TTS components loaded.")# --- Helper Functions for Model Inference ---# 1. Text Generationdef generate_text_response(prompt_text):try:# Phi-4-reasoning-plus uses a specific chat template# System prompt from the model card (simplified for general use here)system_prompt = "You are a helpful AI assistant. Please provide a clear and friendly answer."# The model card suggests a more complex system prompt for reasoning tasks.# For a general chatbot, a simpler one might be fine, or adjust as needed.# Original prompt: "أجب على السؤال التالي بطريقة ودية وواضحة:"# We will use the user's prompt directly with a general system prompt.messages = [{"role": "system", "content": system_prompt},{"role": "user", "content": f"أجب على السؤال التالي بطريقة ودية وواضحة: {prompt_text}"}]# Tokenize the chat# The model card for Phi-4-reasoning-plus shows: inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")# However, apply_chat_template might not be available or work the same for all AutoTokenizers directly without specific setup.# A more general approach for models expecting chat format:prompt_for_model = text_tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = text_tokenizer(prompt_for_model, return_tensors="pt", padding=True, truncation=True, max_length=2048).to(text_model.device)outputs = text_model.generate(**inputs,max_new_tokens=300, # Increased from 200 for potentially longer answers# Parameters from Phi-4 model card for reasoning, can be adjusted for chat# temperature=0.8,# top_p=0.95,# do_sample=True,# For more deterministic chat, we might use different settingstemperature=0.7,top_k=50,do_sample=True,pad_token_id=text_tokenizer.eos_token_id)# Decode, skipping the prompt partresponse_text = text_tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)return response_text.strip()except Exception as e:print(f"Error in text generation: {e}")return f"خطأ في معالجة النص: {str(e)}"# 2. Image Analysisdef analyze_image(pil_image, question_text=None):try:if pil_image is None:return "الرجاء رفع صورة أولاً."# Convert numpy array from Gradio to PIL Image if necessaryif isinstance(pil_image, np.ndarray):pil_image = Image.fromarray(pil_image).convert("RGB")else: # Assuming it's already a PIL image path or object from gr.Imagepil_image = pil_image.convert("RGB")if not question_text or question_text.strip() == "":prompt_for_model = "USER: <image>\nصف محتوى الصورة بالتفصيل\nASSISTANT:"else:prompt_for_model = f"USER: <image>\n{question_text}\nASSISTANT:"inputs = image_processor(text=prompt_for_model, images=pil_image, return_tensors="pt").to(image_model.device)outputs = image_model.generate(**inputs, max_new_tokens=100)response_text = image_processor.decode(outputs[0], skip_special_tokens=True)# LLaVA response often includes the prompt, so we might need to clean it.# Typically, the response starts after "ASSISTANT: "assistant_marker = "ASSISTANT:"if assistant_marker in response_text:response_text = response_text.split(assistant_marker, 1)[-1].strip()return response_textexcept Exception as e:print(f"Error in image analysis: {e}")return f"خطأ في تحليل الصورة: {str(e)}"# 3. Audio Processing (STT and TTS)def process_audio(audio_input):try:if audio_input is None:1 unmodified linesample_rate, audio_data = audio_input# Ensure audio_data is float32 and normalized for Whisperif audio_data.dtype != np.float32:audio_data = audio_data.astype(np.float32)if np.max(np.abs(audio_data)) > 0:audio_data = audio_data / np.max(np.abs(audio_data))else: # Handle silent audioreturn "تم استقبال صوت صامت.", "", (16000, np.array([], dtype=np.int16))# Speech-to-Textinput_features = stt_processor(audio_data, sampling_rate=sample_rate, return_tensors="pt").input_features.to(device)# Generate token idspredicted_ids = stt_model.generate(input_features, language="ar") # Specify language for better accuracy if known# Decode token ids to texttranscription = stt_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]transcription = transcription.strip()if not transcription:return "لم يتمكن النموذج من استخراج نص من الصوت.", "", (16000, np.array([], dtype=np.int16))# Text Generation (Response to transcription)text_response = generate_text_response(transcription)# Text-to-Speechinputs = tts_processor(text=text_response, return_tensors="pt")speech_values = tts_model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=tts_vocoder)# Ensure output is a NumPy array and handle sample rateaudio_output_np = speech_values.cpu().numpy()# SpeechT5 typically outputs at 16kHztts_sample_rate = 16000return transcription, text_response, (tts_sample_rate, audio_output_np)except Exception as e:print(f"Error in audio processing: {e}")# Return empty audio with a valid sample rate for Gradioempty_audio_data = np.array([], dtype=np.float32)return f"خطأ في معالجة الصوت: {str(e)}", "", (16000, empty_audio_data)2 unmodified linestry:if file_obj is None:return "الرجاء رفع ملف أولاً."file_path = file_obj.name # Gradio File object has a .name attribute with the temp pathtext_content = ""if file_path.endswith(".pdf"):with fitz.open(file_path) as doc:10 unmodified linesif not text_content.strip():return "الملف فارغ أو لا يمكن قراءة محتواه النصي."# Summarize or query the content# Limit context size for the model if necessarymax_context_len = 1500 # Adjust based on model limits and typical file sizesif len(text_content) > max_context_len:text_content = text_content[:max_context_len] + "... [المحتوى تم اختصاره]"response = generate_text_response(f"لخص المحتوى التالي من الملف: \n\n{text_content}")return responseexcept Exception as e:print(f"Error processing file: {e}")return f"خطأ في قراءة الملف: {str(e)}"# --- Gradio Interface ---with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial; color: #333; padding: 20px;}", theme=gr.themes.Soft()) as demo:gr.Markdown("# 🤖 Kemo Chat V2 - مساعد ذكي متعدد الوسائط (بأدوات Hugging Face الأحدث)")gr.Markdown("🎯 تفاعل معي عبر النصوص، الصور، الصوت أو الملفات! يدعم العربية والإنجليزية (النماذج قد تتفاوت في دعم اللغات).")gr.Markdown("📁 يدعم الملفات: PDF، Excel، CSV\n🖼️ يدعم الوصف الذكي للصور والسؤال عنها\n🎙️ تحويل الصوت إلى نص والرد صوتياً")with gr.Tab("💬 المحادثة النصية"):text_input = gr.Textbox(label="اكتب سؤالك أو رسالتك هنا", lines=3)1 unmodified linetext_submit = gr.Button("إرسال", variant="primary")text_submit.click(fn=generate_text_response, inputs=text_input, outputs=text_output)with gr.Tab("🖼️ تحليل الصور"):gr.Markdown("ارفع صورة واسأل عنها أو اطلب وصفًا لها.")with gr.Row():image_input = gr.Image(type="pil", label="ارفع صورة") # type="pil" for PIL Image objectwith gr.Column():image_question = gr.Textbox(label="اختياري: اسأل سؤال عن الصورة", lines=2)image_output = gr.Textbox(label="الوصف أو الإجابة", lines=5, interactive=False)image_submit = gr.Button("تحليل الصورة", variant="primary")image_submit.click(fn=analyze_image, inputs=[image_input, image_question], outputs=image_output)with gr.Tab("🎤 التفاعل الصوتي"):gr.Markdown("سجّل رسالة صوتية، سيتم تحويلها إلى نص، ثم الرد عليها نصيًا وصوتيًا.")audio_input = gr.Audio(sources=["microphone"], type="numpy", label="سجّل رسالتك الصوتية") # type="numpy" for (sr, data)with gr.Row():audio_transcription = gr.Textbox(label="النص المستخرج من صوتك", interactive=False, lines=2)audio_text_response = gr.Textbox(label="الرد النصي على رسالتك", interactive=False, lines=3)audio_output_player = gr.Audio(label="الرد الصوتي من المساعد", type="numpy", interactive=False) # type="numpy" for (sr, data)audio_submit = gr.Button("معالجة الصوت", variant="primary")audio_submit.click(fn=process_audio,inputs=audio_input,7 unmodified linesfile_submit.click(fn=process_file, inputs=file_input, outputs=file_output)if __name__ == "__main__":print("Launching Gradio Demo...")demo.launch(share=True) # share=True for public link, remove if not needed8 unmodified lines91011121314151617181920212223245 unmodified lines303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351 unmodified line1371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691702 unmodified lines17317417517617717817910 unmodified lines1901911921931941951961971981992002012022032042052062072082092101 unmodified line2122132142152162172182192202212222232242252262272282292302312322332347 unmodified lines2422432442452462478 unmodified linesfrom transformers import (AutoTokenizer,AutoModelForCausalLM,# AutoProcessor, # Replaced by ViltProcessor for VQA# AutoModelForVision2Seq, # Replaced by ViltForQuestionAnswering for VQAWhisperProcessor,WhisperForConditionalGeneration,ViltProcessor, # Added for ViLT VQA modelViltForQuestionAnswering # Added for ViLT VQA model)from parler_tts import ParlerTTSForConditionalGenerationfrom transformers import AutoFeatureExtractorimport osimport scipy.io.wavfile as wavfileimport io5 unmodified linesif token:login(token=token)else:print("تحذير: لم يتم تعيين متغير البيئة HF_API_TOKEN. بعض النماذج قد تتطلب تسجيل الدخول.")# Device Configurationdevice = "cuda" if torch.cuda.is_available() else "cpu"print(f"استخدام الجهاز: {device}")# 1. Text Generation Model: distilgpt2 (Lightweight)text_model_name = "distilgpt2"print(f"تحميل نموذج النص: {text_model_name}")text_tokenizer = AutoTokenizer.from_pretrained(text_model_name)text_model = AutoModelForCausalLM.from_pretrained(text_model_name,torch_dtype=torch.float32,device_map="auto")if text_tokenizer.pad_token is None:text_tokenizer.pad_token = text_tokenizer.eos_tokenprint("تم تحميل نموذج النص.")# 2. Image Analysis Model: dandelin/vilt-b32-finetuned-vqa (Lightweight, Public VQA)image_model_name = "dandelin/vilt-b32-finetuned-vqa"print(f"تحميل نموذج الصور (VQA): {image_model_name}")image_processor = ViltProcessor.from_pretrained(image_model_name)image_model = ViltForQuestionAnswering.from_pretrained(image_model_name,torch_dtype=torch.float16 if device == "cuda" else torch.float32,device_map="auto")print("تم تحميل نموذج الصور (VQA).")# 3. Speech-to-Text Model: openai/whisper-tiny (Lightweight)stt_model_name = "openai/whisper-tiny"print(f"تحميل نموذج تحويل الكلام إلى نص: {stt_model_name}")stt_processor = WhisperProcessor.from_pretrained(stt_model_name)stt_model = WhisperForConditionalGeneration.from_pretrained(stt_model_name).to(device)stt_model.config.forced_decoder_ids = Noneprint("تم تحميل نموذج تحويل الكلام إلى نص.")# 4. Text-to-Speech Model: parler-tts/parler-tts-tiny-v1 (Lightweight)tts_model_repo_id = "parler-tts/parler-tts-tiny-v1"print(f"تحميل نموذج تحويل النص إلى كلام: {tts_model_repo_id}")tts_model = ParlerTTSForConditionalGeneration.from_pretrained(tts_model_repo_id).to(device)tts_feature_extractor = AutoFeatureExtractor.from_pretrained(tts_model_repo_id)print("تم تحميل مكونات تحويل النص إلى كلام.")# --- Helper Functions for Model Inference ---# 1. Text Generation (using distilgpt2)def generate_text_response(prompt_text):try:full_prompt = f"السؤال: {prompt_text}\nالإجابة الودية والواضحة:"inputs = text_tokenizer(full_prompt, return_tensors="pt", padding=True, truncation=True, max_length=512).to(text_model.device)outputs = text_model.generate(**inputs,max_new_tokens=150,temperature=0.7,top_k=50,do_sample=True,pad_token_id=text_tokenizer.eos_token_id,no_repeat_ngram_size=2)response_text = text_tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)return response_text.strip()except Exception as e:print(f"خطأ في توليد النص: {e}")return f"خطأ في معالجة النص: {str(e)}"# 2. Image Analysis (using dandelin/vilt-b32-finetuned-vqa)def analyze_image(pil_image, question_text=None):try:if pil_image is None:return "الرجاء رفع صورة أولاً."if isinstance(pil_image, np.ndarray):pil_image = Image.fromarray(pil_image).convert("RGB")else:pil_image = pil_image.convert("RGB")if not question_text or question_text.strip() == "":# ViLT is a VQA model, it needs a question.# If no question, we can ask a generic one, or return a message.# For now, let's ask a generic question if none is provided.question_text = "What is in this image?"# Prepare inputs for ViLTencoding = image_processor(pil_image, question_text, return_tensors="pt").to(image_model.device)# Forward passwith torch.no_grad():outputs = image_model(**encoding)logits = outputs.logitsidx = logits.argmax(-1).item()response_text = image_model.config.id2label[idx]return response_textexcept Exception as e:print(f"خطأ في تحليل الصورة: {e}")return f"خطأ في تحليل الصورة: {str(e)}"# 3. Audio Processing (STT with Whisper Tiny and TTS with ParlerTTS Tiny)def process_audio(audio_input):try:if audio_input is None:1 unmodified linesample_rate, audio_data = audio_inputif audio_data.dtype != np.float32:audio_data = audio_data.astype(np.float32)if np.max(np.abs(audio_data)) > 0:audio_data = audio_data / np.max(np.abs(audio_data))else:return "تم استقبال صوت صامت.", "", (16000, np.array([], dtype=np.int16))input_features = stt_processor(audio_data, sampling_rate=sample_rate, return_tensors="pt").input_features.to(device)predicted_ids = stt_model.generate(input_features, language="ar")transcription = stt_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0].strip()if not transcription:return "لم يتمكن النموذج من استخراج نص من الصوت.", "", (16000, np.array([], dtype=np.int16))text_response = generate_text_response(transcription)prompt = text_responsewith torch.no_grad():generation_output = tts_model.generate(input_ids=None,prompt=prompt,do_sample=True,temperature=1.0).cpu().numpy().squeeze()audio_output_np = generation_outputtts_sample_rate = tts_model.config.sampling_ratereturn transcription, text_response, (tts_sample_rate, audio_output_np)except Exception as e:print(f"خطأ في معالجة الصوت: {e}")empty_audio_data = np.array([], dtype=np.float32)return f"خطأ في معالجة الصوت: {str(e)}", "", (16000, empty_audio_data)2 unmodified linestry:if file_obj is None:return "الرجاء رفع ملف أولاً."file_path = file_obj.nametext_content = ""if file_path.endswith(".pdf"):with fitz.open(file_path) as doc:10 unmodified linesif not text_content.strip():return "الملف فارغ أو لا يمكن قراءة محتواه النصي."max_context_len = 1000if len(text_content) > max_context_len:text_content = text_content[:max_context_len] + "... [المحتوى تم اختصاره]"response = generate_text_response(f"لخص المحتوى التالي من الملف: \n\n{text_content}")return responseexcept Exception as e:print(f"خطأ في معالجة الملف: {e}")return f"خطأ في قراءة الملف: {str(e)}"# --- Gradio Interface ---with gr.Blocks(css=".gradio-container {background-color: #f0f4f8; font-family: Arial; color: #333; padding: 20px;}", theme=gr.themes.Soft()) as demo:gr.Markdown("# 🤖 Kemo Chat V3.2 - مساعد ذكي متعدد الوسائط (نماذج خفيفة الوزن - ViLT VQA)")gr.Markdown("🎯 تفاعل معي عبر النصوص، الصور، الصوت أو الملفات! (باستخدام نماذج أقل استهلاكًا للذاكرة).")gr.Markdown("📁 يدعم الملفات: PDF، Excel، CSV\n🖼️ يدعم الإجابة على الأسئلة حول الصور (VQA)\n🎙️ تحويل الصوت إلى نص والرد صوتياً")with gr.Tab("💬 المحادثة النصية"):text_input = gr.Textbox(label="اكتب سؤالك أو رسالتك هنا", lines=3)1 unmodified linetext_submit = gr.Button("إرسال", variant="primary")text_submit.click(fn=generate_text_response, inputs=text_input, outputs=text_output)with gr.Tab("🖼️ تحليل الصور (سؤال وجواب)"):gr.Markdown("ارفع صورة واطرح سؤالاً عنها.")with gr.Row():image_input = gr.Image(type="pil", label="ارفع صورة")with gr.Column():image_question = gr.Textbox(label="اطرح سؤالاً عن الصورة (مطلوب لـ ViLT)", lines=2, placeholder="مثال: What color is the car?")image_output = gr.Textbox(label="الإجابة", lines=5, interactive=False)image_submit = gr.Button("تحليل الصورة", variant="primary")image_submit.click(fn=analyze_image, inputs=[image_input, image_question], outputs=image_output)with gr.Tab("🎤 التفاعل الصوتي"):gr.Markdown("سجّل رسالة صوتية، سيتم تحويلها إلى نص، ثم الرد عليها نصيًا وصوتيًا.")audio_input = gr.Audio(sources=["microphone"], type="numpy", label="سجّل رسالتك الصوتية")with gr.Row():audio_transcription = gr.Textbox(label="النص المستخرج من صوتك", interactive=False, lines=2)audio_text_response = gr.Textbox(label="الرد النصي على رسالتك", interactive=False, lines=3)audio_output_player = gr.Audio(label="الرد الصوتي من المساعد", type="numpy", interactive=False)audio_submit = gr.Button("معالجة الصوت", variant="primary")audio_submit.click(fn=process_audio,inputs=audio_input,7 unmodified linesfile_submit.click(fn=process_file, inputs=file_input, outputs=file_output)if __name__ == "__main__":print("Launching Gradio Demo (Lightweight Models with ViLT VQA)...")demo.launch(share=True)
app.py+89-157