Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,19 +74,6 @@ I never break character and never continue off-topic conversations. I do not imp
|
|
| 74 |
I always return to my purpose: helping people understand their rights.
|
| 75 |
"""
|
| 76 |
|
| 77 |
-
def extract_query_params():
|
| 78 |
-
request_info = os.environ.get("GRADIO_SERVER_REQUEST", "")
|
| 79 |
-
if "?" not in request_info:
|
| 80 |
-
return "anon", None
|
| 81 |
-
parsed = urllib.parse.urlparse(request_info)
|
| 82 |
-
q = urllib.parse.parse_qs(parsed.query)
|
| 83 |
-
uid = q.get("uid", ["anon"])[0]
|
| 84 |
-
cid = q.get("cid", [None])[0]
|
| 85 |
-
return uid, cid
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
# Load chat from file
|
| 90 |
def load_chat(uid, cid):
|
| 91 |
path = f"users/{uid}/{cid}.json"
|
| 92 |
if os.path.exists(path):
|
|
@@ -94,7 +81,6 @@ def load_chat(uid, cid):
|
|
| 94 |
return json.load(f)
|
| 95 |
return []
|
| 96 |
|
| 97 |
-
# Save chat to file
|
| 98 |
def save_chat(uid, cid, history):
|
| 99 |
os.makedirs(f"users/{uid}", exist_ok=True)
|
| 100 |
path = f"users/{uid}/{cid}.json"
|
|
@@ -102,18 +88,14 @@ def save_chat(uid, cid, history):
|
|
| 102 |
json.dump(history, f, indent=2)
|
| 103 |
print(f"[DEBUG] Saved: {path}")
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
def respond(message, history):
|
| 109 |
-
uid, cid = extract_query_params()
|
| 110 |
-
|
| 111 |
if not cid:
|
| 112 |
print("[ERROR] No 'cid' was passed in URL — new file will be created.")
|
| 113 |
cid = uuid.uuid4().hex[:8]
|
| 114 |
|
| 115 |
print(f"[DEBUG] UID: {uid}, CID: {cid}, Gradio history length: {len(history or [])}")
|
| 116 |
-
|
| 117 |
saved_history = load_chat(uid, cid)
|
| 118 |
|
| 119 |
messages = [{"role": "system", "content": miranda_prompt}]
|
|
@@ -123,27 +105,20 @@ def respond(message, history):
|
|
| 123 |
messages.append({"role": "user", "content": message})
|
| 124 |
|
| 125 |
completion = client.chat_completion(
|
| 126 |
-
messages,
|
| 127 |
-
max_tokens=500,
|
| 128 |
-
temperature=0.1,
|
| 129 |
-
stream=False,
|
| 130 |
)
|
| 131 |
response = completion.choices[0].message.content
|
| 132 |
-
|
| 133 |
saved_history.append([message, response])
|
| 134 |
save_chat(uid, cid, saved_history)
|
| 135 |
|
| 136 |
return [{"role": "assistant", "content": response}]
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
app = FastAPI()
|
| 142 |
|
| 143 |
-
# Add CORS so your frontend can call this from another origin (localhost or Vercel)
|
| 144 |
app.add_middleware(
|
| 145 |
CORSMiddleware,
|
| 146 |
-
allow_origins=["*"],
|
| 147 |
allow_credentials=True,
|
| 148 |
allow_methods=["*"],
|
| 149 |
allow_headers=["*"],
|
|
@@ -157,13 +132,21 @@ def list_chats(uid: str):
|
|
| 157 |
files = [f.split(".json")[0] for f in os.listdir(path) if f.endswith(".json")]
|
| 158 |
return files
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
demo = gr.ChatInterface(
|
| 162 |
fn=respond,
|
| 163 |
chatbot=gr.Chatbot(type="messages"),
|
| 164 |
title="Ask Miranda",
|
| 165 |
description="A legal rights assistant to help you understand your rights.",
|
| 166 |
theme="soft"
|
| 167 |
-
).
|
| 168 |
|
| 169 |
-
app =
|
|
|
|
| 74 |
I always return to my purpose: helping people understand their rights.
|
| 75 |
"""
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def load_chat(uid, cid):
|
| 78 |
path = f"users/{uid}/{cid}.json"
|
| 79 |
if os.path.exists(path):
|
|
|
|
| 81 |
return json.load(f)
|
| 82 |
return []
|
| 83 |
|
|
|
|
| 84 |
def save_chat(uid, cid, history):
|
| 85 |
os.makedirs(f"users/{uid}", exist_ok=True)
|
| 86 |
path = f"users/{uid}/{cid}.json"
|
|
|
|
| 88 |
json.dump(history, f, indent=2)
|
| 89 |
print(f"[DEBUG] Saved: {path}")
|
| 90 |
|
| 91 |
+
def respond(message, history, request: gr.Request = None):
|
| 92 |
+
uid = request.query_params.get("uid", "anon")
|
| 93 |
+
cid = request.query_params.get("cid")
|
|
|
|
|
|
|
|
|
|
| 94 |
if not cid:
|
| 95 |
print("[ERROR] No 'cid' was passed in URL — new file will be created.")
|
| 96 |
cid = uuid.uuid4().hex[:8]
|
| 97 |
|
| 98 |
print(f"[DEBUG] UID: {uid}, CID: {cid}, Gradio history length: {len(history or [])}")
|
|
|
|
| 99 |
saved_history = load_chat(uid, cid)
|
| 100 |
|
| 101 |
messages = [{"role": "system", "content": miranda_prompt}]
|
|
|
|
| 105 |
messages.append({"role": "user", "content": message})
|
| 106 |
|
| 107 |
completion = client.chat_completion(
|
| 108 |
+
messages, max_tokens=500, temperature=0.1, stream=False
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
response = completion.choices[0].message.content
|
|
|
|
| 111 |
saved_history.append([message, response])
|
| 112 |
save_chat(uid, cid, saved_history)
|
| 113 |
|
| 114 |
return [{"role": "assistant", "content": response}]
|
| 115 |
|
| 116 |
+
# FastAPI setup
|
|
|
|
|
|
|
| 117 |
app = FastAPI()
|
| 118 |
|
|
|
|
| 119 |
app.add_middleware(
|
| 120 |
CORSMiddleware,
|
| 121 |
+
allow_origins=["*"],
|
| 122 |
allow_credentials=True,
|
| 123 |
allow_methods=["*"],
|
| 124 |
allow_headers=["*"],
|
|
|
|
| 132 |
files = [f.split(".json")[0] for f in os.listdir(path) if f.endswith(".json")]
|
| 133 |
return files
|
| 134 |
|
| 135 |
+
@app.delete("/delete-chat/{uid}/{cid}")
|
| 136 |
+
def delete_chat(uid: str, cid: str):
|
| 137 |
+
path = f"users/{uid}/{cid}.json"
|
| 138 |
+
if os.path.exists(path):
|
| 139 |
+
os.remove(path)
|
| 140 |
+
return {"status": "deleted"}
|
| 141 |
+
return JSONResponse(status_code=404, content={"error": "Chat not found"})
|
| 142 |
+
|
| 143 |
+
# Mount Gradio app
|
| 144 |
demo = gr.ChatInterface(
|
| 145 |
fn=respond,
|
| 146 |
chatbot=gr.Chatbot(type="messages"),
|
| 147 |
title="Ask Miranda",
|
| 148 |
description="A legal rights assistant to help you understand your rights.",
|
| 149 |
theme="soft"
|
| 150 |
+
).queue()
|
| 151 |
|
| 152 |
+
app = mount_gradio_app(app, demo, path="/")
|