Spaces:
Build error
Build error
| import requests | |
| from PIL import Image | |
| import pytesseract | |
| import io | |
| import speech_recognition as sr | |
| import os | |
| import uuid | |
| try: | |
| import moviepy.editor as mp | |
| MOVIEPY_AVAILABLE = True | |
| except ImportError: | |
| print("Warning: moviepy not available, video analysis will be limited") | |
| MOVIEPY_AVAILABLE = False | |
| def analyze_image_from_url(url): | |
| try: | |
| response = requests.get(url, timeout=10) | |
| image = Image.open(io.BytesIO(response.content)) | |
| text = pytesseract.image_to_string(image, lang='eng+ara') | |
| return text.strip() if text.strip() else "ูู ูุชู ุงูุนุซูุฑ ุนูู ูุต ูู ุงูุตูุฑุฉ" | |
| except Exception as e: | |
| return f"ุฎุทุฃ ูู ุชุญููู ุงูุตูุฑุฉ: {str(e)}" | |
| def analyze_audio_from_url(url): | |
| try: | |
| audio_path = f"temp_{uuid.uuid4().hex}.mp3" | |
| with open(audio_path, 'wb') as f: | |
| f.write(requests.get(url, timeout=15).content) | |
| recognizer = sr.Recognizer() | |
| with sr.AudioFile(audio_path) as source: | |
| audio_data = recognizer.record(source) | |
| text = recognizer.recognize_google(audio_data, language="ar") | |
| os.remove(audio_path) | |
| return text | |
| except Exception as e: | |
| return f"ุชุญููู ุงูุตูุช ูุดู: {str(e)}" | |
| def analyze_video_from_url(url): | |
| if not MOVIEPY_AVAILABLE: | |
| return "ุชุญููู ุงูููุฏูู ุบูุฑ ู ุชุงุญ ุญุงููุงู" | |
| try: | |
| video_path = f"temp_{uuid.uuid4().hex}.mp4" | |
| audio_path = f"temp_{uuid.uuid4().hex}.wav" | |
| with open(video_path, 'wb') as f: | |
| f.write(requests.get(url, timeout=20).content) | |
| clip = mp.VideoFileClip(video_path) | |
| clip.audio.write_audiofile(audio_path, codec='pcm_s16le') | |
| recognizer = sr.Recognizer() | |
| with sr.AudioFile(audio_path) as source: | |
| audio_data = recognizer.record(source) | |
| text = recognizer.recognize_google(audio_data, language="ar") | |
| os.remove(video_path) | |
| os.remove(audio_path) | |
| return text | |
| except Exception as e: | |
| return f"ุชุญููู ุงูููุฏูู ูุดู: {str(e)}" | |
| import requests | |
| from PIL import Image | |
| import pytesseract | |
| import io | |
| import speech_recognition as sr | |
| import os | |
| import uuid | |
| import logging | |
| # ุฅุนุฏุงุฏ ุณุฌู ุงูุฃุฎุทุงุก | |
| logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s') | |
| # ุงูุชุญูู ู ู ุชููุฑ ู ูุชุจุฉ moviepy | |
| try: | |
| import moviepy.editor as mp | |
| MOVIEPY_AVAILABLE = True | |
| except ImportError: | |
| logging.warning("moviepy ุบูุฑ ู ุชููุฑุฉุ ุณูุชู ุชุนุทูู ุชุญููู ุงูููุฏูู") | |
| MOVIEPY_AVAILABLE = False | |
| class MediaAnalyzer: | |
| def __init__(self, lang='eng+ara'): | |
| self.lang = lang | |
| def _download_file(self, url, extension): | |
| try: | |
| response = requests.get(url, timeout=20) | |
| response.raise_for_status() | |
| file_path = f"temp_{uuid.uuid4().hex}.{extension}" | |
| with open(file_path, 'wb') as f: | |
| f.write(response.content) | |
| return file_path | |
| except Exception as e: | |
| logging.error(f"ูุดู ุชุญู ูู ุงูู ูู: {e}") | |
| return None | |
| def analyze_image_from_url(self, url): | |
| try: | |
| response = requests.get(url, timeout=10) | |
| image = Image.open(io.BytesIO(response.content)) | |
| text = pytesseract.image_to_string(image, lang=self.lang) | |
| return text.strip() if text.strip() else "ูู ูุชู ุงูุนุซูุฑ ุนูู ูุต ูู ุงูุตูุฑุฉ" | |
| except Exception as e: | |
| logging.error(f"ุฎุทุฃ ูู ุชุญููู ุงูุตูุฑุฉ: {e}") | |
| return f"ุฎุทุฃ ูู ุชุญููู ุงูุตูุฑุฉ: {str(e)}" | |
| def analyze_audio_from_url(self, url): | |
| audio_path = self._download_file(url, "mp3") | |
| if not audio_path: | |
| return "ูุดู ุชุญู ูู ุงูุตูุช" | |
| try: | |
| recognizer = sr.Recognizer() | |
| with sr.AudioFile(audio_path) as source: | |
| audio_data = recognizer.record(source) | |
| text = recognizer.recognize_google(audio_data, language="ar") | |
| return text | |
| except Exception as e: | |
| logging.error(f"ุชุญููู ุงูุตูุช ูุดู: {e}") | |
| return f"ุชุญููู ุงูุตูุช ูุดู: {str(e)}" | |
| finally: | |
| if os.path.exists(audio_path): | |
| os.remove(audio_path) | |
| def analyze_video_from_url(self, url): | |
| if not MOVIEPY_AVAILABLE: | |
| return "ุชุญููู ุงูููุฏูู ุบูุฑ ู ุชุงุญ ุญุงููุงู" | |
| video_path = self._download_file(url, "mp4") | |
| if not video_path: | |
| return "ูุดู ุชุญู ูู ุงูููุฏูู" | |
| audio_path = f"temp_{uuid.uuid4().hex}.wav" | |
| try: | |
| with mp.VideoFileClip(video_path) as clip: | |
| clip.audio.write_audiofile(audio_path, codec='pcm_s16le') | |
| recognizer = sr.Recognizer() | |
| with sr.AudioFile(audio_path) as source: | |
| audio_data = recognizer.record(source) | |
| text = recognizer.recognize_google(audio_data, language="ar") | |
| return text | |
| except Exception as e: | |
| logging.error(f"ุชุญููู ุงูููุฏูู ูุดู: {e}") | |
| return f"ุชุญููู ุงูููุฏูู ูุดู: {str(e)}" | |
| finally: | |
| for path in [video_path, audio_path]: | |
| if os.path.exists(path): | |
| os.remove(path) | |