yusir4200 commited on
Commit
ca2983c
·
verified ·
1 Parent(s): 777a4c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -36,8 +36,38 @@
36
  # iface.launch(share=True, inline=True)
37
  import gradio as gr
38
  from segmentation import segment_image
39
- from medgemma_api import query_medgemma
40
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # 默认图片路径
43
  default_image_path = "./image.png"
 
36
  # iface.launch(share=True, inline=True)
37
  import gradio as gr
38
  from segmentation import segment_image
 
39
  import os
40
+ import os
41
+ import requests
42
+ import base64
43
+
44
+ # 获取 Hugging Face Token(已通过 Secrets 设置)
45
+ HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
46
+ API_URL = "https://api-inference.huggingface.co/models/google/medgemma-4b-it"
47
+ HEADERS = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
48
+
49
+ def query_medgemma(image_path, question):
50
+ with open(image_path, "rb") as f:
51
+ image_bytes = f.read()
52
+ encoded_image = base64.b64encode(image_bytes).decode("utf-8")
53
+
54
+ payload = {
55
+ "inputs": [
56
+ {
57
+ "role": "user",
58
+ "content": [
59
+ {"type": "image", "image": encoded_image},
60
+ {"type": "text", "text": question}
61
+ ]
62
+ }
63
+ ]
64
+ }
65
+
66
+ response = requests.post(API_URL, headers=HEADERS, json=payload)
67
+ if response.ok:
68
+ return response.json()[0]["generated_text"]
69
+ else:
70
+ return f"Error: {response.text}"
71
 
72
  # 默认图片路径
73
  default_image_path = "./image.png"