artificialguybr commited on
Commit
9b6b2fb
1 Parent(s): 8998372

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -4,7 +4,9 @@ import os
4
  import base64
5
  from PIL import Image
6
  import io
7
- import json
 
 
8
 
9
  def resize_image(image_path, max_size=(800, 800), quality=85):
10
  with Image.open(image_path) as img:
@@ -18,16 +20,19 @@ def filepath_to_base64(image_path):
18
  img_base64 = base64.b64encode(img_bytes)
19
  return img_base64.decode('utf-8')
20
 
21
- api_key = os.getenv('API_KEY')
 
 
 
22
 
23
  def call_deplot_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024):
24
  image_base64 = filepath_to_base64(image_path)
25
  invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/3bc390c7-eeec-40f7-a64d-0c6a719985f7"
 
26
  headers = {
27
  "Authorization": f"Bearer {api_key}",
28
  "Accept": "application/json",
29
  }
30
-
31
  payload = {
32
  "messages": [
33
  {
@@ -40,24 +45,16 @@ def call_deplot_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=
40
  "max_tokens": max_tokens,
41
  "stream": False
42
  }
43
-
44
  session = requests.Session()
45
  response = session.post(invoke_url, headers=headers, json=payload)
46
-
47
  while response.status_code == 202:
48
  request_id = response.headers.get("NVCF-REQID")
49
  fetch_url = f"https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/{request_id}"
50
  response = session.get(fetch_url, headers=headers)
51
-
52
  response.raise_for_status()
53
  response_body = response.json()
54
- print(response_body)
55
-
56
- # Processar a resposta conforme necessário
57
- # Este exemplo apenas imprime a resposta, mas você pode adaptá-lo conforme necessário
58
- return response_body
59
 
60
- # Configuração da Interface Gráfica
61
  content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
62
  image_input = gr.Image(type="filepath", label="Upload Image")
63
  temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
@@ -75,7 +72,6 @@ iface = gr.Interface(fn=call_deplot_api,
75
  <p>
76
  Utilize Google DePlot to translate images of plots or charts into linearized tables. This one-shot visual language understanding solution offers a unique approach to interpreting visual data.
77
  </p>
78
- ...
79
  """
80
  )
81
 
 
4
  import base64
5
  from PIL import Image
6
  import io
7
+
8
+ api_key = os.getenv('API_KEY')
9
+
10
 
11
  def resize_image(image_path, max_size=(800, 800), quality=85):
12
  with Image.open(image_path) as img:
 
20
  img_base64 = base64.b64encode(img_bytes)
21
  return img_base64.decode('utf-8')
22
 
23
+ def format_response(response_body):
24
+ content = response_body['choices'][0]['message']['content']
25
+ formatted_content = content.replace("<0x0A>", "\n")
26
+ return formatted_content
27
 
28
  def call_deplot_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024):
29
  image_base64 = filepath_to_base64(image_path)
30
  invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/3bc390c7-eeec-40f7-a64d-0c6a719985f7"
31
+ api_key = os.getenv('API_KEY')
32
  headers = {
33
  "Authorization": f"Bearer {api_key}",
34
  "Accept": "application/json",
35
  }
 
36
  payload = {
37
  "messages": [
38
  {
 
45
  "max_tokens": max_tokens,
46
  "stream": False
47
  }
 
48
  session = requests.Session()
49
  response = session.post(invoke_url, headers=headers, json=payload)
 
50
  while response.status_code == 202:
51
  request_id = response.headers.get("NVCF-REQID")
52
  fetch_url = f"https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/{request_id}"
53
  response = session.get(fetch_url, headers=headers)
 
54
  response.raise_for_status()
55
  response_body = response.json()
56
+ return format_response(response_body)
 
 
 
 
57
 
 
58
  content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
59
  image_input = gr.Image(type="filepath", label="Upload Image")
60
  temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
 
72
  <p>
73
  Utilize Google DePlot to translate images of plots or charts into linearized tables. This one-shot visual language understanding solution offers a unique approach to interpreting visual data.
74
  </p>
 
75
  """
76
  )
77