mindcraft-vllm / app.py
sanvrypt's picture
Update app.py
89fd841 verified
from flask import Flask, request, jsonify, Response
import requests
import traceback
app = Flask(__name__)
# ==============================
# CONFIG
# ==============================
OLLAMA_URL = "http://localhost:11434"
DEFAULT_MODEL = "sweaterdog/andy-4:latest"
# ==============================
# CORS
# ==============================
@app.after_request
def after_request(response):
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "Content-Type"
response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
return response
# ==============================
# HOME
# ==============================
@app.route("/")
def home():
return f"""
<!DOCTYPE html>
<html>
<head>
<title>Andy-4 API</title>
</head>
<body style="font-family:Arial;padding:50px;text-align:center;">
<h1>🤖 Andy-4 API Running</h1>
<p><b>Model:</b> {DEFAULT_MODEL}</p>
<h3>Test CURL</h3>
<pre style="background:#f4f4f4;padding:20px;text-align:left;max-width:900px;margin:auto;">
curl -X POST https://sanvrypt-mindcraft-vllm.hf.space/api/generate \\
-H "Content-Type: application/json" \\
-d '{{
"prompt": "Halo Andy-4!",
"stream": false
}}'
</pre>
<button onclick="testAPI()">Test API</button>
<pre id="result"
style="margin-top:20px;padding:20px;background:#f4f4f4;text-align:left;white-space:pre-wrap;"></pre>
<script>
async function testAPI() {{
const el = document.getElementById("result");
el.textContent = "Testing...";
try {{
const res = await fetch("/api/generate", {{
method: "POST",
headers: {{ "Content-Type": "application/json" }},
body: JSON.stringify({{
prompt: "Hello from browser!",
stream: false
}})
}});
const data = await res.json();
el.textContent = JSON.stringify(data, null, 2);
}} catch (e) {{
el.textContent = "ERROR: " + e.message;
}}
}}
</script>
</body>
</html>
"""
# ==============================
# GENERATE
# ==============================
@app.route("/api/generate", methods=["POST", "OPTIONS"])
def generate():
if request.method == "OPTIONS":
return "", 204
try:
data = request.get_json(force=True)
# FORCE MODEL
data["model"] = DEFAULT_MODEL
resp = requests.post(
f"{OLLAMA_URL}/api/generate",
json=data,
stream=data.get("stream", False),
timeout=300
)
if data.get("stream", False):
def gen():
for line in resp.iter_lines():
if line:
yield line + b"\n"
return Response(gen(), content_type="application/x-ndjson")
return jsonify(resp.json())
except Exception as e:
traceback.print_exc()
return jsonify({"error": str(e)}), 500
# ==============================
# CHAT
# ==============================
@app.route("/api/chat", methods=["POST", "OPTIONS"])
def chat():
if request.method == "OPTIONS":
return "", 204
try:
data = request.get_json(force=True)
# FORCE MODEL
data["model"] = DEFAULT_MODEL
resp = requests.post(
f"{OLLAMA_URL}/api/chat",
json=data,
stream=data.get("stream", False),
timeout=300
)
if data.get("stream", False):
def gen():
for line in resp.iter_lines():
if line:
yield line + b"\n"
return Response(gen(), content_type="application/x-ndjson")
return jsonify(resp.json())
except Exception as e:
traceback.print_exc()
return jsonify({"error": str(e)}), 500
# ==============================
# FALLBACK
# ==============================
@app.route("/<path:path>", methods=["GET", "POST"])
def catch_all(path):
return jsonify({
"error": "Not Found",
"path": path
}), 404
# ==============================
# MAIN
# ==============================
if __name__ == "__main__":
print("=" * 50)
print(" Andy-4 Flask API ")
print(f" Model : {DEFAULT_MODEL}")
print(" Port : 7860")
print("=" * 50)
app.run(
host="0.0.0.0",
port=7860,
debug=False
)