Spaces:
Sleeping
Sleeping
File size: 1,557 Bytes
562b017 330b634 562b017 e6b49c3 562b017 9bebba9 562b017 9bebba9 562b017 |
1 2 3 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 38 39 40 41 42 43 44 45 46 47 48 |
import os
import requests
from flask import Flask, render_template, request, jsonify
# 配置 Flask
app = Flask(__name__)
UPLOAD_FOLDER = "static/uploads"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
# 你的推理服务器地址
SERVER_URL = "http://127.0.0.1:5000/infer" # 如果你的服务器在远程,改为公网 IP 或 ngrok 地址
# 确保上传目录存在
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
# 获取上传的图片和文本
image = request.files["image"]
text = request.form["text"]
conv_mode = request.form.get("conv_mode", "vicuna_v1")
if image:
image_path = os.path.join(app.config["UPLOAD_FOLDER"], image.filename)
image.save(image_path) # 保存图片
# 发送请求到推理服务器
payload = {
"image_path": image_path,
"text": text,
"conv_mode": conv_mode
}
response = requests.post(SERVER_URL, json=payload)
if response.status_code == 200:
result = response.json()["response"]
else:
result = "Error: Server did not respond correctly."
return render_template("index.html", image_url=image_path, text=text, result=result)
return render_template("index.html", image_url=None, text="", result="")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True)
|