Spaces:
Sleeping
Sleeping
Commit ·
89f7aa3
1
Parent(s): b4c425a
init commit
Browse files
app.py
CHANGED
|
@@ -1,31 +1,14 @@
|
|
| 1 |
-
"""
|
| 2 |
-
app.py — HF Space frontend server
|
| 3 |
-
===================================
|
| 4 |
-
Lives in the Hugging Face Space. Does two things only:
|
| 5 |
-
|
| 6 |
-
1. Serves index.html (and static assets) to the browser
|
| 7 |
-
2. Proxies POST /generate to the remote Gradio server running on your machine,
|
| 8 |
-
translating between the frontend's JSON schema and Gradio's /run/predict API
|
| 9 |
-
|
| 10 |
-
Set GRADIO_SERVER_URL to the public *.gradio.live URL that server.py prints
|
| 11 |
-
when it starts (or your own domain if you're running behind a reverse proxy).
|
| 12 |
-
|
| 13 |
-
Architecture
|
| 14 |
-
------------
|
| 15 |
-
Browser → POST /generate (HF Space) → POST /run/predict (your machine)
|
| 16 |
-
app.py server.py
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
from flask import Flask, request, jsonify, send_from_directory
|
| 20 |
import requests
|
| 21 |
import os
|
| 22 |
|
| 23 |
app = Flask(__name__, static_folder="static")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
GRADIO_SERVER_URL = "https://c9ceb67adbae7238f6.gradio.live"
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
@app.route("/")
|
|
@@ -35,19 +18,27 @@ def index():
|
|
| 35 |
|
| 36 |
@app.route("/static/<path:filename>")
|
| 37 |
def static_files(filename):
|
| 38 |
-
return send_from_directory(
|
| 39 |
|
| 40 |
|
| 41 |
@app.route("/generate", methods=["POST"])
|
| 42 |
def generate():
|
| 43 |
-
body = request.get_json(
|
| 44 |
|
| 45 |
-
prompt = body.get("prompt", "").strip()
|
| 46 |
if not prompt:
|
| 47 |
return jsonify({"error": "prompt is required"}), 400
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
# [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
gradio_payload = {
|
| 52 |
"data": [
|
| 53 |
prompt,
|
|
@@ -59,18 +50,25 @@ def generate():
|
|
| 59 |
}
|
| 60 |
|
| 61 |
try:
|
| 62 |
-
resp = requests.post(
|
| 63 |
resp.raise_for_status()
|
| 64 |
except requests.exceptions.ConnectionError:
|
| 65 |
-
return jsonify({"error": "Cannot reach inference server.
|
| 66 |
except requests.exceptions.Timeout:
|
| 67 |
return jsonify({"error": "Inference server timed out."}), 504
|
| 68 |
except requests.exceptions.HTTPError as e:
|
| 69 |
-
return jsonify({"error": f"Inference server error: {e}"}), 502
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
generated_text = resp.json()["data"][0]
|
| 72 |
return jsonify({"generated_text": generated_text, "prompt": prompt})
|
| 73 |
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = Flask(__name__, static_folder="static")
|
| 6 |
|
| 7 |
+
# Put your *.gradio.live or *.hf.space URL (no trailing slash)
|
| 8 |
+
GRADIO_SERVER_URL = os.getenv("GRADIO_SERVER_URL", "https://c9ceb67adbae7238f6.gradio.live").rstrip("/")
|
| 9 |
|
| 10 |
+
# Default endpoint — some Spaces expose /predict by default
|
| 11 |
+
GRADIO_PREDICT_URL = f"{GRADIO_SERVER_URL}/predict"
|
| 12 |
|
| 13 |
|
| 14 |
@app.route("/")
|
|
|
|
| 18 |
|
| 19 |
@app.route("/static/<path:filename>")
|
| 20 |
def static_files(filename):
|
| 21 |
+
return send_from_directory(app.static_folder, filename)
|
| 22 |
|
| 23 |
|
| 24 |
@app.route("/generate", methods=["POST"])
|
| 25 |
def generate():
|
| 26 |
+
body = request.get_json(silent=True) or {}
|
| 27 |
|
| 28 |
+
prompt = str(body.get("prompt", "")).strip()
|
| 29 |
if not prompt:
|
| 30 |
return jsonify({"error": "prompt is required"}), 400
|
| 31 |
|
| 32 |
+
# You can optionally override if the Space uses a custom API name.
|
| 33 |
+
# Example: body["api_name"] = "/generate" or "/my_api"
|
| 34 |
+
api_name = body.get("api_name", "/predict")
|
| 35 |
+
if not api_name.startswith("/"):
|
| 36 |
+
api_name = "/" + api_name
|
| 37 |
+
|
| 38 |
+
api_url = f"{GRADIO_SERVER_URL}{api_name}"
|
| 39 |
+
|
| 40 |
+
# Gradio expects positional arguments if the endpoint is /predict
|
| 41 |
+
# Usually it's a list matching the input components
|
| 42 |
gradio_payload = {
|
| 43 |
"data": [
|
| 44 |
prompt,
|
|
|
|
| 50 |
}
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
resp = requests.post(api_url, json=gradio_payload, timeout=120)
|
| 54 |
resp.raise_for_status()
|
| 55 |
except requests.exceptions.ConnectionError:
|
| 56 |
+
return jsonify({"error": "Cannot reach inference server."}), 502
|
| 57 |
except requests.exceptions.Timeout:
|
| 58 |
return jsonify({"error": "Inference server timed out."}), 504
|
| 59 |
except requests.exceptions.HTTPError as e:
|
| 60 |
+
return jsonify({"error": f"Inference server error: {str(e)}", "response": resp.text}), 502
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
result = resp.json()
|
| 64 |
+
# for many Spaces, the first item in "data" is the output
|
| 65 |
+
generated_text = result.get("data", [None])[0] if isinstance(result, dict) else None
|
| 66 |
+
except Exception:
|
| 67 |
+
return jsonify({"error": "Unexpected inference server response", "raw": resp.text}), 502
|
| 68 |
|
|
|
|
| 69 |
return jsonify({"generated_text": generated_text, "prompt": prompt})
|
| 70 |
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
port = int(os.getenv("PORT", "7860"))
|
| 74 |
+
app.run(port=port, debug=False)
|