R-Kentaren commited on
Commit
2809502
·
verified ·
1 Parent(s): bee5c55

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +0 -131
app.py CHANGED
@@ -1,10 +1,7 @@
1
  import os
2
  import json
3
- import uuid
4
- import time
5
  import re
6
  from pathlib import Path
7
- from fastapi import FastAPI
8
  from fastapi.responses import HTMLResponse
9
  from fastapi.staticfiles import StaticFiles
10
  import gradio as gr
@@ -13,14 +10,6 @@ from openai import OpenAI
13
  # Default system prompt for DeepSeek V4 Flash
14
  DEFAULT_SYSTEM_PROMPT = "You are DeepSeek, a helpful AI assistant powered by DeepSeek V4 Flash. You provide accurate, detailed, and thoughtful responses."
15
 
16
- # ── In-memory per-user session storage ──────────────────────────────────
17
- USER_DATA = {}
18
-
19
- def get_user_sessions(username):
20
- if username not in USER_DATA:
21
- USER_DATA[username] = {"sessions": {}, "active_session": None}
22
- return USER_DATA[username]
23
-
24
  # ── Initialize Gradio Server (FastAPI subclass) ─────────────────────────
25
  app = gr.Server()
26
 
@@ -93,126 +82,6 @@ def chat_with_deepseek(
93
  })
94
 
95
 
96
- # ── API Endpoint: Get User Info (OAuth) ────────────────────────────────
97
- @app.api(name="get_user_info")
98
- def get_user_info(profile: gr.OAuthProfile | None = None) -> str:
99
- """Return the logged-in user's name, or empty string if not logged in."""
100
- # The Gradio Server passes the OAuth profile automatically
101
- if profile is None:
102
- return json.dumps({"logged_in": False, "username": ""})
103
- return json.dumps({"logged_in": True, "username": getattr(profile, 'name', '')})
104
-
105
-
106
- # ── API Endpoint: Save Chat Session ────────────────────────────────────
107
- @app.api(name="save_chat_session")
108
- def save_chat_session(
109
- messages_json: str,
110
- title: str,
111
- session_id: str,
112
- profile: gr.OAuthProfile | None = None,
113
- ) -> str:
114
- if profile is None:
115
- return json.dumps({"status": "error", "message": "Not logged in"})
116
-
117
- username = getattr(profile, 'name', '')
118
- ud = get_user_sessions(username)
119
-
120
- if not session_id or session_id == "new":
121
- sid = str(uuid.uuid4())[:8]
122
- else:
123
- sid = session_id
124
-
125
- messages = json.loads(messages_json) if messages_json else []
126
-
127
- if not title or title == "New Chat":
128
- for m in messages:
129
- if m.get("role") == "user":
130
- t = m.get("content", "")
131
- if isinstance(t, str):
132
- title = t[:60] + ("..." if len(t) > 60 else "")
133
- break
134
-
135
- ud["sessions"][sid] = {
136
- "title": title,
137
- "messages": messages,
138
- "updated_at": time.time()
139
- }
140
- ud["active_session"] = sid
141
-
142
- return json.dumps({"status": "success", "session_id": sid, "title": title})
143
-
144
-
145
- # ── API Endpoint: Load Chat Session ────────────────────────────────────
146
- @app.api(name="load_chat_session")
147
- def load_chat_session(
148
- session_id: str,
149
- profile: gr.OAuthProfile | None = None,
150
- ) -> str:
151
- if profile is None:
152
- return json.dumps({"status": "error", "message": "Not logged in"})
153
-
154
- username = getattr(profile, 'name', '')
155
- ud = get_user_sessions(username)
156
-
157
- if session_id not in ud["sessions"]:
158
- return json.dumps({"status": "error", "message": "Session not found"})
159
-
160
- ud["active_session"] = session_id
161
- sess = ud["sessions"][session_id]
162
-
163
- return json.dumps({
164
- "status": "success",
165
- "session_id": session_id,
166
- "title": sess["title"],
167
- "messages": sess["messages"]
168
- })
169
-
170
-
171
- # ── API Endpoint: List Chat Sessions ───────────────────────────────────
172
- @app.api(name="list_chat_sessions")
173
- def list_chat_sessions(profile: gr.OAuthProfile | None = None) -> str:
174
- if profile is None:
175
- return json.dumps({"status": "error", "message": "Not logged in", "sessions": []})
176
-
177
- username = getattr(profile, 'name', '')
178
- ud = get_user_sessions(username)
179
-
180
- sessions = []
181
- for sid, sess in sorted(
182
- ud["sessions"].items(),
183
- key=lambda x: x[1].get("updated_at", 0),
184
- reverse=True
185
- ):
186
- sessions.append({
187
- "id": sid,
188
- "title": sess["title"],
189
- "updated_at": sess.get("updated_at", 0),
190
- "message_count": len(sess.get("messages", []))
191
- })
192
-
193
- return json.dumps({"status": "success", "sessions": sessions})
194
-
195
-
196
- # ── API Endpoint: Delete Chat Session ──────────────────────────────────
197
- @app.api(name="delete_chat_session")
198
- def delete_chat_session(
199
- session_id: str,
200
- profile: gr.OAuthProfile | None = None,
201
- ) -> str:
202
- if profile is None:
203
- return json.dumps({"status": "error", "message": "Not logged in"})
204
-
205
- username = getattr(profile, 'name', '')
206
- ud = get_user_sessions(username)
207
-
208
- if session_id in ud["sessions"]:
209
- del ud["sessions"][session_id]
210
- if ud["active_session"] == session_id:
211
- ud["active_session"] = None
212
-
213
- return json.dumps({"status": "success"})
214
-
215
-
216
  # ── Serve the main HTML page ────────────────────────────────────────────
217
  @app.get("/")
218
  async def homepage():
 
1
  import os
2
  import json
 
 
3
  import re
4
  from pathlib import Path
 
5
  from fastapi.responses import HTMLResponse
6
  from fastapi.staticfiles import StaticFiles
7
  import gradio as gr
 
10
  # Default system prompt for DeepSeek V4 Flash
11
  DEFAULT_SYSTEM_PROMPT = "You are DeepSeek, a helpful AI assistant powered by DeepSeek V4 Flash. You provide accurate, detailed, and thoughtful responses."
12
 
 
 
 
 
 
 
 
 
13
  # ── Initialize Gradio Server (FastAPI subclass) ─────────────────────────
14
  app = gr.Server()
15
 
 
82
  })
83
 
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  # ── Serve the main HTML page ────────────────────────────────────────────
86
  @app.get("/")
87
  async def homepage():