Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
| 3 |
from gradio_client import Client
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
# Required for Flask sessions
|
| 8 |
-
app.secret_key = os.environ.get("FLASK_SECRET_KEY")
|
| 9 |
-
|
| 10 |
client = Client("tonyassi/llm-api", token=os.environ.get("HF_TOKEN"))
|
| 11 |
API_NAME = "/chat"
|
| 12 |
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
# per-session chat history
|
| 16 |
-
if "chat_history" not in session:
|
| 17 |
-
session["chat_history"] = []
|
| 18 |
-
return session["chat_history"]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def set_history(history):
|
| 22 |
-
session["chat_history"] = history
|
| 23 |
-
session.modified = True
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def llm_response(user_input: str) -> str:
|
| 27 |
user_input = (user_input or "").strip()
|
| 28 |
if not user_input:
|
| 29 |
return "Type something first"
|
| 30 |
|
| 31 |
-
history =
|
| 32 |
history.append({"role": "user", "content": user_input})
|
| 33 |
-
set_history(history)
|
| 34 |
|
| 35 |
try:
|
| 36 |
assistant_text = client.predict(
|
|
@@ -41,18 +28,16 @@ def llm_response(user_input: str) -> str:
|
|
| 41 |
if isinstance(assistant_text, str) and "-***-" in assistant_text:
|
| 42 |
assistant_text = assistant_text.split("-***-")[0].strip()
|
| 43 |
|
| 44 |
-
history = get_history()
|
| 45 |
history.append({"role": "assistant", "content": assistant_text})
|
| 46 |
-
|
| 47 |
|
| 48 |
return assistant_text
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
# rollback last user message on failure
|
| 52 |
-
history = get_history()
|
| 53 |
if history and history[-1].get("role") == "user":
|
| 54 |
history.pop()
|
| 55 |
-
|
| 56 |
|
| 57 |
print("LLM API error:", e)
|
| 58 |
return "Something isn't working..."
|
|
@@ -60,22 +45,29 @@ def llm_response(user_input: str) -> str:
|
|
| 60 |
|
| 61 |
@app.route("/")
|
| 62 |
def index():
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
@app.route("/send_message", methods=["POST"])
|
| 69 |
def send_message():
|
| 70 |
-
user_input = request.form
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return jsonify({"response": response, "user_input": user_input})
|
| 74 |
|
| 75 |
|
| 76 |
@app.route("/reset", methods=["POST"])
|
| 77 |
def reset():
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
return jsonify({"ok": True})
|
| 80 |
|
| 81 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import uuid
|
| 3 |
+
from flask import Flask, render_template, request, jsonify
|
| 4 |
from gradio_client import Client
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
client = Client("tonyassi/llm-api", token=os.environ.get("HF_TOKEN"))
|
| 9 |
API_NAME = "/chat"
|
| 10 |
|
| 11 |
+
# In-memory store: resets if Space restarts (fine for your use-case)
|
| 12 |
+
HISTORIES = {}
|
| 13 |
|
| 14 |
+
def llm_response(session_id: str, user_input: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
user_input = (user_input or "").strip()
|
| 16 |
if not user_input:
|
| 17 |
return "Type something first"
|
| 18 |
|
| 19 |
+
history = HISTORIES.get(session_id, [])
|
| 20 |
history.append({"role": "user", "content": user_input})
|
|
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
assistant_text = client.predict(
|
|
|
|
| 28 |
if isinstance(assistant_text, str) and "-***-" in assistant_text:
|
| 29 |
assistant_text = assistant_text.split("-***-")[0].strip()
|
| 30 |
|
|
|
|
| 31 |
history.append({"role": "assistant", "content": assistant_text})
|
| 32 |
+
HISTORIES[session_id] = history
|
| 33 |
|
| 34 |
return assistant_text
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
# rollback last user message on failure
|
|
|
|
| 38 |
if history and history[-1].get("role") == "user":
|
| 39 |
history.pop()
|
| 40 |
+
HISTORIES[session_id] = history
|
| 41 |
|
| 42 |
print("LLM API error:", e)
|
| 43 |
return "Something isn't working..."
|
|
|
|
| 45 |
|
| 46 |
@app.route("/")
|
| 47 |
def index():
|
| 48 |
+
# New session id on every page load => refresh starts over
|
| 49 |
+
session_id = uuid.uuid4().hex
|
| 50 |
+
HISTORIES.pop(session_id, None)
|
| 51 |
+
return render_template("index.html", session_id=session_id)
|
| 52 |
|
| 53 |
|
| 54 |
@app.route("/send_message", methods=["POST"])
|
| 55 |
def send_message():
|
| 56 |
+
user_input = request.form.get("user_input", "")
|
| 57 |
+
session_id = request.form.get("session_id", "")
|
| 58 |
+
|
| 59 |
+
if not session_id:
|
| 60 |
+
return jsonify({"response": "Missing session_id", "user_input": user_input}), 400
|
| 61 |
+
|
| 62 |
+
response = llm_response(session_id, user_input)
|
| 63 |
return jsonify({"response": response, "user_input": user_input})
|
| 64 |
|
| 65 |
|
| 66 |
@app.route("/reset", methods=["POST"])
|
| 67 |
def reset():
|
| 68 |
+
session_id = request.form.get("session_id", "")
|
| 69 |
+
if session_id:
|
| 70 |
+
HISTORIES.pop(session_id, None)
|
| 71 |
return jsonify({"ok": True})
|
| 72 |
|
| 73 |
|