Yanqing0327 commited on
Commit
a722746
·
verified ·
1 Parent(s): b24b85e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -45
app.py CHANGED
@@ -1,47 +1,34 @@
1
- import os
2
  import requests
3
- from flask import Flask, render_template, request, jsonify
4
 
5
- # 配置 Flask
6
- app = Flask(__name__)
7
- UPLOAD_FOLDER = "static/uploads"
8
- app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
9
-
10
- # 你的推理服务器地址
11
- SERVER_URL = "http://127.0.0.1:5000/infer" # 如果你的服务器在远程,改为公网 IP 或 ngrok 地址
12
-
13
- # 确保上传目录存在
14
- if not os.path.exists(UPLOAD_FOLDER):
15
- os.makedirs(UPLOAD_FOLDER)
16
-
17
- @app.route("/", methods=["GET", "POST"])
18
- def index():
19
- if request.method == "POST":
20
- # 获取上传的图片和文本
21
- image = request.files["image"]
22
- text = request.form["text"]
23
- conv_mode = request.form.get("conv_mode", "vicuna_v1")
24
-
25
- if image:
26
- image_path = os.path.join(app.config["UPLOAD_FOLDER"], image.filename)
27
- image.save(image_path) # 保存图片
28
-
29
- # 发送请求到推理服务器
30
- payload = {
31
- "image_path": image_path,
32
- "text": text,
33
- "conv_mode": conv_mode
34
- }
35
- response = requests.post(SERVER_URL, json=payload)
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)