Spaces:
Sleeping
Sleeping
Commit ·
52106ca
1
Parent(s): 13d012b
init commit
Browse files- Dockerfile +3 -4
- app.py +49 -53
Dockerfile
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
#
|
| 2 |
-
# you will also find guides on how best to write your Dockerfile
|
| 3 |
-
|
| 4 |
FROM python:3.9
|
| 5 |
|
| 6 |
RUN useradd -m -u 1000 user
|
|
@@ -13,4 +11,5 @@ COPY --chown=user ./requirements.txt requirements.txt
|
|
| 13 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
|
| 15 |
COPY --chown=user . /app
|
| 16 |
-
|
|
|
|
|
|
| 1 |
+
# HF Space — frontend only, no model weights needed here
|
|
|
|
|
|
|
| 2 |
FROM python:3.9
|
| 3 |
|
| 4 |
RUN useradd -m -u 1000 user
|
|
|
|
| 11 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 12 |
|
| 13 |
COPY --chown=user . /app
|
| 14 |
+
|
| 15 |
+
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:7860", "--timeout", "120"]
|
app.py
CHANGED
|
@@ -1,42 +1,31 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
=============================
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
Usage
|
| 19 |
-
-----
|
| 20 |
-
pip install flask transformers torch
|
| 21 |
-
python app.py
|
| 22 |
-
# Open http://localhost:5000
|
| 23 |
"""
|
| 24 |
|
| 25 |
from flask import Flask, request, jsonify, send_from_directory
|
| 26 |
-
|
| 27 |
-
import torch
|
| 28 |
import os
|
| 29 |
|
| 30 |
app = Flask(__name__, static_folder="static")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
model = GPT2LMHeadModel.from_pretrained("helloadhavan/llara1.1-100M-base")
|
| 35 |
-
model.eval()
|
| 36 |
-
print("Model ready.")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
model.to(DEVICE)
|
| 40 |
|
| 41 |
|
| 42 |
@app.route("/")
|
|
@@ -44,6 +33,11 @@ def index():
|
|
| 44 |
return send_from_directory(".", "index.html")
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
@app.route("/generate", methods=["POST"])
|
| 48 |
def generate():
|
| 49 |
body = request.get_json(force=True)
|
|
@@ -52,29 +46,31 @@ def generate():
|
|
| 52 |
if not prompt:
|
| 53 |
return jsonify({"error": "prompt is required"}), 400
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
app.run(debug=True,
|
|
|
|
| 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 |
+
# Paste the *.gradio.live URL printed by server.py here (no trailing slash)
|
| 26 |
+
GRADIO_SERVER_URL = os.environ.get("GRADIO_SERVER_URL", "https://your-tunnel.gradio.live")
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
GRADIO_PREDICT_URL = f"{GRADIO_SERVER_URL}/run/predict"
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
@app.route("/")
|
|
|
|
| 33 |
return send_from_directory(".", "index.html")
|
| 34 |
|
| 35 |
|
| 36 |
+
@app.route("/static/<path:filename>")
|
| 37 |
+
def static_files(filename):
|
| 38 |
+
return send_from_directory("static", filename)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
@app.route("/generate", methods=["POST"])
|
| 42 |
def generate():
|
| 43 |
body = request.get_json(force=True)
|
|
|
|
| 46 |
if not prompt:
|
| 47 |
return jsonify({"error": "prompt is required"}), 400
|
| 48 |
|
| 49 |
+
# Gradio expects inputs as a positional list matching the server.py fn signature:
|
| 50 |
+
# [prompt, max_new_tokens, temperature, top_p, repetition_penalty]
|
| 51 |
+
gradio_payload = {
|
| 52 |
+
"data": [
|
| 53 |
+
prompt,
|
| 54 |
+
int(body.get("max_new_tokens", 200)),
|
| 55 |
+
float(body.get("temperature", 0.9)),
|
| 56 |
+
float(body.get("top_p", 0.95)),
|
| 57 |
+
float(body.get("repetition_penalty", 1.2)),
|
| 58 |
+
]
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
resp = requests.post(GRADIO_PREDICT_URL, json=gradio_payload, timeout=120)
|
| 63 |
+
resp.raise_for_status()
|
| 64 |
+
except requests.exceptions.ConnectionError:
|
| 65 |
+
return jsonify({"error": "Cannot reach inference server. Is server.py running?"}), 502
|
| 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 |
+
app.run(debug=True, port=7860)
|