loayshabet commited on
Commit
81f7613
·
verified ·
1 Parent(s): f1ebd99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -4,10 +4,12 @@ from PIL import Image
4
  import io
5
  import base64
6
 
 
7
  OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
8
- OPENROUTER_API_KEY = "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"
9
  MODEL_NAME = "Mistral: Magistral Medium 2506 (thinking)"
10
 
 
11
  SYSTEM_PROMPT = """
12
  You are a professional technical analyst with 10 years of experience in financial markets.
13
  Analyze the chart provided and answer the following:
@@ -22,18 +24,21 @@ Analyze the chart provided and answer the following:
22
  Answer in Arabic in a professional tone.
23
  """
24
 
 
25
  def image_to_data_uri(img):
26
  buffered = io.BytesIO()
27
- img.save(buffered, format=img.format)
28
- return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode()
 
29
 
 
30
  def analyze_chart(image):
31
  if image is None:
32
  return "❌ لم يتم تحميل أي صورة."
33
-
34
- data_uri = image_to_data_uri(image)
35
 
36
  try:
 
 
37
  response = requests.post(
38
  OPENROUTER_API_URL,
39
  headers={
@@ -54,10 +59,13 @@ def analyze_chart(image):
54
  "max_tokens": 512
55
  }
56
  )
 
57
  result = response.json()
58
  return result["choices"][0]["message"]["content"]
 
59
  except Exception as e:
60
  return f"❌ خطأ: {str(e)}"
61
 
 
62
  interface = gr.Interface(fn=analyze_chart, inputs=gr.Image(type="pil"), outputs="markdown")
63
  interface.launch()
 
4
  import io
5
  import base64
6
 
7
+ # إعدادات API
8
  OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
9
+ OPENROUTER_API_KEY = "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" # ← استبدلها بـ API Key الخاص بك
10
  MODEL_NAME = "Mistral: Magistral Medium 2506 (thinking)"
11
 
12
+ # تعليمات الذكاء الاصطناعي
13
  SYSTEM_PROMPT = """
14
  You are a professional technical analyst with 10 years of experience in financial markets.
15
  Analyze the chart provided and answer the following:
 
24
  Answer in Arabic in a professional tone.
25
  """
26
 
27
+ # دالة تحويل الصورة إلى Data URI
28
  def image_to_data_uri(img):
29
  buffered = io.BytesIO()
30
+ img_format = img.format if img.format else "PNG"
31
+ img.save(buffered, format=img_format)
32
+ return "data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode("utf-8")
33
 
34
+ # دالة التحليل
35
  def analyze_chart(image):
36
  if image is None:
37
  return "❌ لم يتم تحميل أي صورة."
 
 
38
 
39
  try:
40
+ data_uri = image_to_data_uri(image)
41
+
42
  response = requests.post(
43
  OPENROUTER_API_URL,
44
  headers={
 
59
  "max_tokens": 512
60
  }
61
  )
62
+
63
  result = response.json()
64
  return result["choices"][0]["message"]["content"]
65
+
66
  except Exception as e:
67
  return f"❌ خطأ: {str(e)}"
68
 
69
+ # واجهة Gradio
70
  interface = gr.Interface(fn=analyze_chart, inputs=gr.Image(type="pil"), outputs="markdown")
71
  interface.launch()