Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,34 @@
|
|
1 |
-
import
|
2 |
import requests
|
3 |
-
from flask import Flask, render_template, request, jsonify
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
if response.status_code == 200:
|
38 |
-
result = response.json()["response"]
|
39 |
-
else:
|
40 |
-
result = "Error: Server did not respond correctly."
|
41 |
-
|
42 |
-
return render_template("index.html", image_url=image_path, text=text, result=result)
|
43 |
-
|
44 |
-
return render_template("index.html", image_url=None, text="", result="")
|
45 |
-
|
46 |
-
if __name__ == "__main__":
|
47 |
-
app.run(host="0.0.0.0", port=8080, debug=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
import requests
|
|
|
3 |
|
4 |
+
# 你的 ngrok 地址(确保它正确)
|
5 |
+
API_URL = "https://8725-169-233-7-2.ngrok-free.app/predict/"
|
6 |
+
|
7 |
+
def chat(image, text):
|
8 |
+
"""调用本地 LLaVA 服务器的 API"""
|
9 |
+
if image is None:
|
10 |
+
return "请上传图片"
|
11 |
+
|
12 |
+
# 保存图片为临时文件
|
13 |
+
image.save("temp.jpg")
|
14 |
+
|
15 |
+
files = {"image": open("temp.jpg", "rb")}
|
16 |
+
data = {"text": text}
|
17 |
+
|
18 |
+
try:
|
19 |
+
response = requests.post(API_URL, files=files, data=data)
|
20 |
+
return response.json().get("response", "错误:无法解析响应")
|
21 |
+
except Exception as e:
|
22 |
+
return f"请求失败: {str(e)}"
|
23 |
+
|
24 |
+
# 创建 Gradio 界面
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=chat,
|
27 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(lines=2, placeholder="输入你的问题")],
|
28 |
+
outputs=gr.Textbox(),
|
29 |
+
title="LLaVA Visual Chatbot",
|
30 |
+
description="上传图片并输入问题,LLaVA 将回答你的问题。",
|
31 |
+
)
|
32 |
+
|
33 |
+
# 启动 Gradio
|
34 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|