Spaces:
Sleeping
Sleeping
Create app.py
#1
by hichem123 - opened
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import pytesseract
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# جلب مفتاح OpenRouter من إعدادات السرية
|
| 8 |
+
api_key = os.getenv("OPENROUTER_API_KEY", "ضع_مفتاحك_هنا_مؤقتًا_للتجربة")
|
| 9 |
+
|
| 10 |
+
# إعداد OpenRouter API
|
| 11 |
+
client = OpenAI(
|
| 12 |
+
api_key=api_key,
|
| 13 |
+
base_url="https://openrouter.ai/api/v1"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# دعم اللغات
|
| 17 |
+
language_prompts = {
|
| 18 |
+
"Arabic": "رجاءً اشرح نتائج التحليل بلغة عربية مبسطة وسهلة الفهم، ووضح ما إذا كانت القيم طبيعية.",
|
| 19 |
+
"French": "Veuillez expliquer les résultats en français simple et clair, en indiquant si les valeurs sont normales.",
|
| 20 |
+
"English": "Please explain the results in simple and clear English, and indicate if the values are normal."
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
ocr_languages = {
|
| 24 |
+
"Arabic": "ara",
|
| 25 |
+
"French": "fra",
|
| 26 |
+
"English": "eng"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# الدالة الرئيسية
|
| 30 |
+
def process_image(image, language):
|
| 31 |
+
# OCR حسب اللغة المختارة
|
| 32 |
+
ocr_lang = ocr_languages.get(language, "eng")
|
| 33 |
+
extracted_text = pytesseract.image_to_string(image, lang=ocr_lang)
|
| 34 |
+
|
| 35 |
+
if not extracted_text.strip():
|
| 36 |
+
return "❗ لم يتم التعرف على أي نص في الصورة.", ""
|
| 37 |
+
|
| 38 |
+
# إعداد الموجه حسب اللغة
|
| 39 |
+
prompt = f"""
|
| 40 |
+
These are medical test results:
|
| 41 |
+
|
| 42 |
+
{extracted_text}
|
| 43 |
+
|
| 44 |
+
{language_prompts[language]}
|
| 45 |
+
Do not provide a medical diagnosis, only a clear explanation.
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
response = client.chat.completions.create(
|
| 50 |
+
model="mistralai/mixtral-8x7b-instruct", # يمكن تغييره إلى "openai/gpt-3.5-turbo"
|
| 51 |
+
messages=[{"role": "user", "content": prompt}]
|
| 52 |
+
)
|
| 53 |
+
explanation = response.choices[0].message.content
|
| 54 |
+
except Exception as e:
|
| 55 |
+
explanation = f"❌ خطأ أثناء الاتصال بـ OpenRouter:\n{e}"
|
| 56 |
+
|
| 57 |
+
return extracted_text, explanation
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# واجهة Gradio
|
| 61 |
+
interface = gr.Interface(
|
| 62 |
+
fn=process_image,
|
| 63 |
+
inputs=[
|
| 64 |
+
gr.Image(type="pil", label="📤 ارفع صورة التحليل الطبي"),
|
| 65 |
+
gr.Radio(choices=["Arabic", "French", "English"], label="🌐 اختر اللغة", value="Arabic")
|
| 66 |
+
],
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.Textbox(label="📋 النص المستخرج من الصورة"),
|
| 69 |
+
gr.Textbox(label="💬 الشرح الطبي")
|
| 70 |
+
],
|
| 71 |
+
title="🩺 Talk2Doc - مساعد الذكاء لشرح التحاليل",
|
| 72 |
+
description="ارفع صورة تحليل طبي، واختر لغة الشرح (عربية، فرنسية، أو إنجليزية)."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# إطلاق الواجهة
|
| 76 |
+
interface.launch()
|