tonyassi commited on
Commit
c9b0017
·
verified ·
1 Parent(s): 546f120

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -30
app.py CHANGED
@@ -1,36 +1,23 @@
1
  import os
2
- from flask import Flask, render_template, request, jsonify, session
 
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 get_history():
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 = get_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
- set_history(history)
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
- set_history(history)
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
- # Refreshing the page hits "/", so this clears the chat
64
- session.pop("chat_history", None)
65
- return render_template("index.html")
 
66
 
67
 
68
  @app.route("/send_message", methods=["POST"])
69
  def send_message():
70
- user_input = request.form["user_input"]
71
- response = llm_response(user_input)
72
- print("history len:", len(session.get("chat_history", [])))
 
 
 
 
73
  return jsonify({"response": response, "user_input": user_input})
74
 
75
 
76
  @app.route("/reset", methods=["POST"])
77
  def reset():
78
- session.pop("chat_history", None)
 
 
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