root
		
	commited on
		
		
					Commit 
							
							·
						
						a7d9e19
	
1
								Parent(s):
							
							667cb5b
								
add token
Browse files- app.py +19 -10
- assets/logo.jpg +2 -2
    	
        app.py
    CHANGED
    
    | @@ -10,7 +10,8 @@ import gradio as gr | |
| 10 | 
             
            # =========================
         | 
| 11 | 
             
            # Config
         | 
| 12 | 
             
            # =========================
         | 
| 13 | 
            -
            DEFAULT_API_URL = | 
|  | |
| 14 | 
             
            LOGO_IMAGE_PATH = './assets/logo.jpg'
         | 
| 15 | 
             
            GOOGLE_FONTS_URL = "<link href='https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap' rel='stylesheet'>"
         | 
| 16 | 
             
            LATEX_DELIMS = [
         | 
| @@ -19,7 +20,8 @@ LATEX_DELIMS = [ | |
| 19 | 
             
                {"left": "\\(", "right": "\\)", "display": False},
         | 
| 20 | 
             
                {"left": "\\[", "right": "\\]", "display": True},
         | 
| 21 | 
             
            ]
         | 
| 22 | 
            -
             | 
|  | |
| 23 | 
             
            # =========================
         | 
| 24 | 
             
            # Base64 and Example Loading Logic (From New Script)
         | 
| 25 | 
             
            # =========================
         | 
| @@ -85,30 +87,37 @@ def _file_to_b64_image_only(file_path: str) -> Tuple[str, int]: | |
| 85 | 
             
                with open(file_path, "rb") as f:
         | 
| 86 | 
             
                    return base64.b64encode(f.read()).decode("utf-8"), 1
         | 
| 87 |  | 
| 88 | 
            -
            def _call_api(api_url: str, file_path: str, use_layout_detection: bool, | 
|  | |
| 89 | 
             
                b64, file_type = _file_to_b64_image_only(file_path)
         | 
| 90 | 
            -
                payload = { | 
| 91 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
| 92 | 
             
                if not use_layout_detection:
         | 
| 93 | 
            -
                    if not prompt_label: | 
|  | |
| 94 | 
             
                    payload["promptLabel"] = prompt_label.strip().lower()
         | 
| 95 | 
            -
             | 
| 96 | 
            -
                # This parameter is from the new script's logic
         | 
| 97 | 
             
                if use_layout_detection and use_chart_recognition:
         | 
| 98 | 
             
                    payload["use_chart_recognition"] = True
         | 
| 99 |  | 
| 100 | 
             
                try:
         | 
| 101 | 
            -
                     | 
|  | |
| 102 | 
             
                    resp.raise_for_status()
         | 
| 103 | 
             
                    data = resp.json()
         | 
| 104 | 
             
                except requests.exceptions.RequestException as e:
         | 
| 105 | 
             
                    raise gr.Error(f"API request failed:{e}")
         | 
| 106 | 
             
                except json.JSONDecodeError:
         | 
| 107 | 
             
                    raise gr.Error(f"Invalid JSON response from server:\n{getattr(resp, 'text', '')}")
         | 
|  | |
| 108 | 
             
                if data.get("errorCode", -1) != 0:
         | 
| 109 | 
            -
                    raise gr.Error( | 
| 110 | 
             
                return data
         | 
| 111 |  | 
|  | |
| 112 | 
             
            # =========================
         | 
| 113 | 
             
            # Core Logic for Handling Image URLs (From Old "Xinghe" Script)
         | 
| 114 | 
             
            # =========================
         | 
|  | |
| 10 | 
             
            # =========================
         | 
| 11 | 
             
            # Config
         | 
| 12 | 
             
            # =========================
         | 
| 13 | 
            +
            DEFAULT_API_URL =os.environ.get("API_URL")
         | 
| 14 | 
            +
            TOKEN = os.environ.get("TOKEN")
         | 
| 15 | 
             
            LOGO_IMAGE_PATH = './assets/logo.jpg'
         | 
| 16 | 
             
            GOOGLE_FONTS_URL = "<link href='https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap' rel='stylesheet'>"
         | 
| 17 | 
             
            LATEX_DELIMS = [
         | 
|  | |
| 20 | 
             
                {"left": "\\(", "right": "\\)", "display": False},
         | 
| 21 | 
             
                {"left": "\\[", "right": "\\]", "display": True},
         | 
| 22 | 
             
            ]
         | 
| 23 | 
            +
            AUTH_HEADER = {"Authorization": f"bearer {TOKEN}"}
         | 
| 24 | 
            +
            JSON_HEADERS = {**AUTH_HEADER, "Content-Type": "application/json"}
         | 
| 25 | 
             
            # =========================
         | 
| 26 | 
             
            # Base64 and Example Loading Logic (From New Script)
         | 
| 27 | 
             
            # =========================
         | 
|  | |
| 87 | 
             
                with open(file_path, "rb") as f:
         | 
| 88 | 
             
                    return base64.b64encode(f.read()).decode("utf-8"), 1
         | 
| 89 |  | 
| 90 | 
            +
            def _call_api(api_url: str, file_path: str, use_layout_detection: bool,
         | 
| 91 | 
            +
                          prompt_label: Optional[str], use_chart_recognition: bool = False) -> Dict[str, Any]:
         | 
| 92 | 
             
                b64, file_type = _file_to_b64_image_only(file_path)
         | 
| 93 | 
            +
                payload = {
         | 
| 94 | 
            +
                    "file": b64,
         | 
| 95 | 
            +
                    "useLayoutDetection": bool(use_layout_detection),
         | 
| 96 | 
            +
                    "fileType": file_type,
         | 
| 97 | 
            +
                    "layoutMergeBboxesMode": "union",
         | 
| 98 | 
            +
                }
         | 
| 99 | 
             
                if not use_layout_detection:
         | 
| 100 | 
            +
                    if not prompt_label:
         | 
| 101 | 
            +
                        raise ValueError("Please select a recognition type.")
         | 
| 102 | 
             
                    payload["promptLabel"] = prompt_label.strip().lower()
         | 
|  | |
|  | |
| 103 | 
             
                if use_layout_detection and use_chart_recognition:
         | 
| 104 | 
             
                    payload["use_chart_recognition"] = True
         | 
| 105 |  | 
| 106 | 
             
                try:
         | 
| 107 | 
            +
                    # 关键改动:带上 headers
         | 
| 108 | 
            +
                    resp = requests.post(api_url, json=payload, headers=JSON_HEADERS, timeout=120)
         | 
| 109 | 
             
                    resp.raise_for_status()
         | 
| 110 | 
             
                    data = resp.json()
         | 
| 111 | 
             
                except requests.exceptions.RequestException as e:
         | 
| 112 | 
             
                    raise gr.Error(f"API request failed:{e}")
         | 
| 113 | 
             
                except json.JSONDecodeError:
         | 
| 114 | 
             
                    raise gr.Error(f"Invalid JSON response from server:\n{getattr(resp, 'text', '')}")
         | 
| 115 | 
            +
             | 
| 116 | 
             
                if data.get("errorCode", -1) != 0:
         | 
| 117 | 
            +
                    raise gr.Error("API returned an error:")
         | 
| 118 | 
             
                return data
         | 
| 119 |  | 
| 120 | 
            +
             | 
| 121 | 
             
            # =========================
         | 
| 122 | 
             
            # Core Logic for Handling Image URLs (From Old "Xinghe" Script)
         | 
| 123 | 
             
            # =========================
         | 
    	
        assets/logo.jpg
    CHANGED
    
    |   | 
| Git LFS Details
 | 
|   | 
| Git LFS Details
 | 
