Sandy2636 commited on
Commit
d82951f
·
1 Parent(s): ca25c3d

Add application file

Browse files
Files changed (1) hide show
  1. app.py +16 -1
app.py CHANGED
@@ -2,10 +2,25 @@ import gradio as gr
2
  import base64
3
  import requests
4
  import json
 
5
 
6
  API_KEY = "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"
7
  IMAGE_MODEL = "opengvlab/internvl3-14b:free"
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def process_passport(image):
10
  try:
11
  with open(image, "rb") as f:
@@ -63,7 +78,7 @@ def process_passport(image):
63
  response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
64
  result = response.json()
65
 
66
- return json.dumps(result["choices"][0]["message"]["content"], indent=2)
67
 
68
  except Exception as e:
69
  return f"⚠️ Error: {str(e)}"
 
2
  import base64
3
  import requests
4
  import json
5
+ import re
6
 
7
  API_KEY = "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"
8
  IMAGE_MODEL = "opengvlab/internvl3-14b:free"
9
 
10
+ def extract_json_from_code_block(text):
11
+ try:
12
+ # Match content between triple backticks with 'json'
13
+ match = re.search(r"```json\s*(\{.*?\})\s*```", text, re.DOTALL)
14
+ if not match:
15
+ return {"error": "No JSON block found."}
16
+
17
+ json_str = match.group(1)
18
+
19
+ # Convert to Python dictionary
20
+ return json.loads(json_str)
21
+ except json.JSONDecodeError as e:
22
+ return {"error": f"Invalid JSON: {str(e)}"}
23
+
24
  def process_passport(image):
25
  try:
26
  with open(image, "rb") as f:
 
78
  response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
79
  result = response.json()
80
 
81
+ return json.dumps(extract_json_from_code_block(result["choices"][0]["message"]["content"]), indent=2)
82
 
83
  except Exception as e:
84
  return f"⚠️ Error: {str(e)}"