Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,47 @@
|
|
|
|
1 |
import requests
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
"text": text,
|
11 |
-
"conv_mode": conv_mode
|
12 |
-
}
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|