Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,120 +1,111 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import os
|
| 4 |
-
import requests
|
| 5 |
-
import random
|
| 6 |
-
|
| 7 |
-
# Serve UI from / (but static files live in 'ui/')
|
| 8 |
-
app = Flask(__name__, static_folder='ui', static_url_path='/')
|
| 9 |
-
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 10 |
-
|
| 11 |
-
# ---------- UI ----------
|
| 12 |
-
@app.route('/', methods=['GET'])
|
| 13 |
-
def index():
|
| 14 |
-
return app.send_static_file('index.html')
|
| 15 |
-
|
| 16 |
-
# Serve assets from /ui/... and fallback to SPA (GET only)
|
| 17 |
-
@app.route('/<path:path>', methods=['GET'])
|
| 18 |
-
def spa_or_asset(path):
|
| 19 |
-
file_path = os.path.join(app.static_folder, path)
|
| 20 |
-
if os.path.isfile(file_path):
|
| 21 |
-
return send_from_directory(app.static_folder, path)
|
| 22 |
-
return send_from_directory(app.static_folder, 'index.html')
|
| 23 |
-
|
| 24 |
-
# Quiet the favicon noise
|
| 25 |
-
@app.route('/favicon.ico', methods=['GET'])
|
| 26 |
-
def favicon():
|
| 27 |
-
return ('', 204)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
id2label = {
|
| 32 |
-
0: "Level 0: mild roast",
|
| 33 |
-
1: "Level 1: emotional shade",
|
| 34 |
-
2: "Level 2: mild damage",
|
| 35 |
-
3: "Level 3: utter destruction",
|
| 36 |
-
4: "Level 4: character defamation",
|
| 37 |
-
5: "Level 5: soul punch",
|
| 38 |
-
6: "Level 6: historic devastation",
|
| 39 |
-
7: "Level 7: irreparable trauma"
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
"
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
#
|
| 102 |
-
@app.route("/
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def list_routes():
|
| 113 |
-
return {"routes": [str(r) for r in app.url_map.iter_rules()]}
|
| 114 |
-
|
| 115 |
-
if __name__ == "__main__":
|
| 116 |
-
print("=== ROUTE MAP ===")
|
| 117 |
-
for r in app.url_map.iter_rules():
|
| 118 |
-
print(r, list(r.methods))
|
| 119 |
-
print("=================")
|
| 120 |
-
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
# Serve UI from / (but static files live in 'ui/')
|
| 8 |
+
app = Flask(__name__, static_folder='ui', static_url_path='/')
|
| 9 |
+
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 10 |
+
|
| 11 |
+
# ---------- UI ----------
|
| 12 |
+
@app.route('/', methods=['GET'])
|
| 13 |
+
def index():
|
| 14 |
+
return app.send_static_file('index.html')
|
| 15 |
+
|
| 16 |
+
# Serve assets from /ui/... and fallback to SPA (GET only)
|
| 17 |
+
@app.route('/<path:path>', methods=['GET'])
|
| 18 |
+
def spa_or_asset(path):
|
| 19 |
+
file_path = os.path.join(app.static_folder, path)
|
| 20 |
+
if os.path.isfile(file_path):
|
| 21 |
+
return send_from_directory(app.static_folder, path)
|
| 22 |
+
return send_from_directory(app.static_folder, 'index.html')
|
| 23 |
+
|
| 24 |
+
# Quiet the favicon noise
|
| 25 |
+
@app.route('/favicon.ico', methods=['GET'])
|
| 26 |
+
def favicon():
|
| 27 |
+
return ('', 204)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
id2label = {
|
| 32 |
+
0: "Level 0: mild roast",
|
| 33 |
+
1: "Level 1: emotional shade",
|
| 34 |
+
2: "Level 2: mild damage",
|
| 35 |
+
3: "Level 3: utter destruction",
|
| 36 |
+
4: "Level 4: character defamation",
|
| 37 |
+
5: "Level 5: soul punch",
|
| 38 |
+
6: "Level 6: historic devastation",
|
| 39 |
+
7: "Level 7: irreparable trauma"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# ---------- OLLAMA BACKEND ----------
|
| 43 |
+
OLLAMA_URL = "http://localhost:11434/api/generate"
|
| 44 |
+
MODEL_NAME = "phi3:mini"
|
| 45 |
+
|
| 46 |
+
def ask_phi(prompt):
|
| 47 |
+
print("[ask_phi] -> Ollama")
|
| 48 |
+
try:
|
| 49 |
+
resp = requests.post(
|
| 50 |
+
OLLAMA_URL,
|
| 51 |
+
json={"model": MODEL_NAME, "prompt": prompt, "stream": False},
|
| 52 |
+
timeout=60
|
| 53 |
+
)
|
| 54 |
+
resp.raise_for_status()
|
| 55 |
+
return resp.json().get("response", "").strip()
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print("Ollama error:", e)
|
| 58 |
+
return "0"
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def _predict_impl():
|
| 62 |
+
data = request.get_json(silent=True) or {}
|
| 63 |
+
text = data.get("text", "").strip()
|
| 64 |
+
if not text:
|
| 65 |
+
return jsonify({"error": "Empty input"}), 400
|
| 66 |
+
|
| 67 |
+
level_prompt = (
|
| 68 |
+
"Classify the emotional damage of the following sentence on a scale from 0 "
|
| 69 |
+
"(mild roast) to 7 (irreparable trauma). Respond with a single digit number only.\n\n"
|
| 70 |
+
f"Sentence: \"{text}\"\nDamage Level:"
|
| 71 |
+
)
|
| 72 |
+
level_raw = ask_phi(level_prompt)
|
| 73 |
+
print("[Level Output]", level_raw)
|
| 74 |
+
|
| 75 |
+
digits = [ch for ch in level_raw if ch.isdigit()]
|
| 76 |
+
level = int(digits[0]) if digits else 0
|
| 77 |
+
level = max(0, min(level, 7))
|
| 78 |
+
|
| 79 |
+
roast_prompt = (
|
| 80 |
+
"You are EmoNet’s snarky alter ego — a sasscore machine trained on sarcasm, late-night roasts, and internet chaos. But today, you’re in your flirty villain era. For any input text, reply with a short, teasingly devastating comeback that mixes charm with chaos. It should feel like emotional damage... but make it hot. Keep it clever, spicy, and seductive — no cruelty, no cringe. Think: if Gen Z flirted like a roast battle and a chaotic therapist whispered pickup lines mid-burn.\n\n"
|
| 81 |
+
f"Sentence: \"{text}\"\nDamage Level: {level}\nRoast:"
|
| 82 |
+
)
|
| 83 |
+
roast = ask_phi(roast_prompt).strip()
|
| 84 |
+
print("[Roast Output]", roast)
|
| 85 |
+
|
| 86 |
+
return jsonify({
|
| 87 |
+
"label": id2label[level],
|
| 88 |
+
"confidence": round(random.uniform(0.45, 0.9), 3),
|
| 89 |
+
"roast": roast
|
| 90 |
+
})
|
| 91 |
+
|
| 92 |
+
# Single handler serves both routes; explicit OPTIONS so preflight never gets hijacked
|
| 93 |
+
@app.route("/predict", methods=["POST", "OPTIONS"])
|
| 94 |
+
@app.route("/api/predict", methods=["POST", "OPTIONS"])
|
| 95 |
+
def predict():
|
| 96 |
+
if request.method == "OPTIONS":
|
| 97 |
+
return ('', 204)
|
| 98 |
+
return _predict_impl()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# Debug helpers: see what routes are live
|
| 102 |
+
@app.route("/__routes", methods=["GET"])
|
| 103 |
+
def list_routes():
|
| 104 |
+
return {"routes": [str(r) for r in app.url_map.iter_rules()]}
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
print("=== ROUTE MAP ===")
|
| 108 |
+
for r in app.url_map.iter_rules():
|
| 109 |
+
print(r, list(r.methods))
|
| 110 |
+
print("=================")
|
| 111 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|