loayshabet commited on
Commit
76b55ef
·
verified ·
1 Parent(s): 3f3f461

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -1,10 +1,11 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from PIL import Image
4
  import io
5
 
6
- # 📦 تحميل النموذج باستخدام Pipeline
7
- pipe = pipeline("image-to-text", model="microsoft/kosmos-2-patch14-224")
 
8
 
9
  # 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
10
  SYSTEM_PROMPT = """
@@ -29,17 +30,21 @@ def analyze_chart(image):
29
  # تحويل الصورة إلى RGB
30
  img = image.convert("RGB")
31
 
32
- # إنشاء prompt شامل
33
- text = f"Question: {SYSTEM_PROMPT}\nAnswer:"
34
 
35
- # التحليل باستخدام Pipeline
36
- response = pipe(img, text)
37
 
38
- # تنظيف الإجابة
39
- return response[0]["generated_text"]
 
 
 
 
40
 
41
  except Exception as e:
42
- return f"❌ حدث خطأ أثناء التحليل: {str(e)}"
43
 
44
  # 🖥️ واجهة Gradio
45
  interface = gr.Interface(
 
1
  import gradio as gr
2
+ from transformers import AutoProcessor, Kosmos2ForConditionalGeneration
3
  from PIL import Image
4
  import io
5
 
6
+ # 📦 تحميل المعالج والنموذج
7
+ processor = AutoProcessor.from_pretrained("microsoft/kosmos-2-patch14-224")
8
+ model = Kosmos2ForConditionalGeneration.from_pretrained("microsoft/kosmos-2-patch14-224")
9
 
10
  # 💬 تعليمات الذكاء الاصطناعي لتكون خبير تداول
11
  SYSTEM_PROMPT = """
 
30
  # تحويل الصورة إلى RGB
31
  img = image.convert("RGB")
32
 
33
+ # دمج التعليمات مع الصورة
34
+ prompt = f"{SYSTEM_PROMPT}\nQuestion: ما هو تحليل هذه الصورة؟\nAnswer:"
35
 
36
+ # المعالجة
37
+ inputs = processor(images=img, text=prompt, return_tensors="pt")
38
 
39
+ # التوليد
40
+ generated_ids = model.generate(**inputs, max_new_tokens=256)
41
+ response = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
42
+ response = processor.tokenizer.decode(processor.tokenizer.encode(response.split("Answer:")[-1].strip()))
43
+
44
+ return response
45
 
46
  except Exception as e:
47
+ return f"❌ حدث خطأ أثناء التحليل:\n{str(e)}"
48
 
49
  # 🖥️ واجهة Gradio
50
  interface = gr.Interface(