sanbo commited on
Commit
7160aa7
·
1 Parent(s): 941cba6

update sth. at 2024-11-15 18:37:03

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from PIL import Image
4
- import requests
5
- import os
6
 
7
  # ===================== 核心逻辑模块 =====================
8
 
9
- # 初始化所需的模型客户端
10
  try:
11
  # 文本聊天模型
12
  client_text = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
@@ -52,14 +50,19 @@ def visual_qa(image, question):
52
  调用视觉文档问答模型回答图像问题。
53
  """
54
  try:
55
- # 如果输入是路径或URL,则直接加载
56
- if isinstance(image, str):
57
- response = client_vqa.visual_question_answering(image=image, question=question)
 
58
  else:
59
- # 将本地图像保存为临时文件以供模型使用
60
- temp_image_path = f"/tmp/{os.path.basename(image.filename)}"
61
- image.save(temp_image_path)
62
- response = client_vqa.visual_question_answering(image=temp_image_path, question=question)
 
 
 
 
63
  return response["answer"]
64
  except Exception as e:
65
  print(f"Visual QA failed: {e}")
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from PIL import Image
 
 
4
 
5
  # ===================== 核心逻辑模块 =====================
6
 
7
+ # 初始化模型客户端
8
  try:
9
  # 文本聊天模型
10
  client_text = InferenceClient("meta-llama/Llama-3.2-11B-Vision-Instruct")
 
50
  调用视觉文档问答模型回答图像问题。
51
  """
52
  try:
53
+ # 检查输入是否为URL路径或本地路径
54
+ if isinstance(image, str) and image.startswith("http"):
55
+ # 如果是网络路径,直接使用
56
+ image_path = image
57
  else:
58
+ # 如果是本地图像,获取Gradio的文件路径
59
+ image_path = image.name # Gradio会传递上传文件的路径
60
+
61
+ # 调用视觉问答API
62
+ response = client_vqa.visual_question_answering(
63
+ image=image_path,
64
+ question=question
65
+ )
66
  return response["answer"]
67
  except Exception as e:
68
  print(f"Visual QA failed: {e}")