qwendashcoder commited on
Commit
531fe80
Β·
verified Β·
1 Parent(s): b9180c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -165
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  from openai import OpenAI
3
  import gradio as gr
4
- from auth import register_user, login_user, logout_user, save_chats
5
 
6
  # ================== YOUR SETTINGS ==================
7
  API_KEY = os.environ.get("API_KEY")
@@ -11,71 +11,35 @@ USER_DATA_REPO = "qwendashcoder/fluade_1"
11
  if not HF_TOKEN:
12
  raise ValueError("HF_TOKEN secret not set in HF Spaces settings!")
13
 
14
- client = OpenAI(
15
- base_url="https://integrate.api.nvidia.com/v1",
16
- api_key=API_KEY
17
- )
18
-
19
- auth_globals = {
20
- "hf_token": HF_TOKEN,
21
- "user_repo": USER_DATA_REPO
22
- }
23
-
24
- model_choices = [
25
- "meta/llama-3.1-405b-instruct",
26
- "meta/llama-3.1-70b-instruct",
27
- "meta/llama-3.1-8b-instruct",
28
- "mistralai/mistral-large-2-instruct",
29
- "mistralai/mixtral-8x22b-instruct",
30
- "mistralai/mistral-nemo-instruct-2407",
31
- "qwen/qwen-2.5-72b-instruct",
32
- "google/gemma-2-27b-it",
33
- "google/gemma-2-9b-it",
34
- "google/codegemma-7b-it",
35
- ]
36
-
37
- deprecated_models = [
38
- "qwen/qwen3-coder-480b-a35b-instruct",
39
- "qwen/qwen2.5-coder-32b-instruct",
40
- ]
41
 
 
 
 
 
42
  model_choices = [m for m in model_choices if m not in deprecated_models]
43
  default_model = model_choices[0] if model_choices else None
44
 
45
  def is_model_available(model_id):
46
  try:
47
- test_completion = client.chat.completions.create(
48
- model=model_id,
49
- messages=[{"role": "user", "content": "Say 'test'"}],
50
- max_tokens=5,
51
- stream=False
52
- )
53
  return test_completion.choices[0].message.content is not None
54
  except Exception:
55
  return False
56
 
57
- available_models = [model for model in model_choices if is_model_available(model)]
58
- if not available_models:
59
- available_models = [default_model] if default_model else []
60
  default_model = available_models[0] if available_models else None
61
 
62
  def load_model(selected_model):
63
  try:
64
- test_completion = client.chat.completions.create(
65
- model=selected_model,
66
- messages=[{"role": "user", "content": "Say 'test'"}],
67
- max_tokens=5,
68
- stream=False
69
- )
70
  if test_completion.choices[0].message.content:
71
  return selected_model, f"βœ… Loaded & Ready: {selected_model}"
72
  except Exception as e:
73
  return None, f"❌ Unavailable: {str(e)}\nTry another model."
74
 
75
- # ================== MULTI-CHAT HELPERS ==================
76
  def create_new_chat(user_chats, logged_in_username):
77
- if not logged_in_username:
78
- return user_chats, None, [], gr.update(choices=[], value=None), "⚠️ Please login first!"
79
  new_num = len(user_chats) + 1
80
  new_title = f"Chat {new_num}"
81
  new_chat = {"title": new_title, "history": []}
@@ -84,16 +48,14 @@ def create_new_chat(user_chats, logged_in_username):
84
  return new_user_chats, new_title, [], gr.update(choices=choices, value=new_title), f"βœ… New chat created: {new_title}"
85
 
86
  def switch_chat(selected_title, user_chats):
87
- if not selected_title or not user_chats:
88
- return [], None
89
  for chat in user_chats:
90
  if chat["title"] == selected_title:
91
  return chat["history"], selected_title
92
  return [], None
93
 
94
  def delete_current_chat(current_title, user_chats, logged_in_username):
95
- if not logged_in_username or not current_title:
96
- return user_chats, current_title, [], gr.update(), "⚠️ Nothing to delete!"
97
  new_user_chats = [chat for chat in user_chats if chat["title"] != current_title]
98
  choices = [chat["title"] for chat in new_user_chats]
99
  if new_user_chats:
@@ -104,83 +66,56 @@ def delete_current_chat(current_title, user_chats, logged_in_username):
104
  new_history = []
105
  return new_user_chats, new_title, new_history, gr.update(choices=choices, value=new_title), f"πŸ—‘οΈ Deleted {current_title}"
106
 
107
- # ================== UPDATE CHATS AFTER STREAMING (fixes persistence) ==================
108
  def update_chats_after_response(user_chats, current_chat_title, final_history, logged_in_username):
109
- if not logged_in_username or not current_chat_title or not user_chats:
110
- return user_chats
111
  for chat in user_chats:
112
  if chat["title"] == current_chat_title:
113
- chat["history"] = final_history[:] # save the full updated history
114
  break
115
  save_chats(logged_in_username, user_chats, auth_globals)
116
  return user_chats
117
 
118
- # ================== CHAT STREAM (streaming only) ==================
119
  def chat_stream(message, history, current_model, logged_in_username, user_chats, current_chat_title):
120
  history = history or []
121
-
122
- if not logged_in_username:
123
  history.append({"role": "assistant", "content": "⚠️ Please login first!"})
124
- yield history
125
- return
126
- if not current_model:
127
  history.append({"role": "assistant", "content": "⚠️ Load a model first!"})
128
- yield history
129
- return
130
- if not current_chat_title:
131
  history.append({"role": "assistant", "content": "⚠️ Select or create a chat first!"})
132
- yield history
133
- return
134
 
135
- # Normal chat
136
  history.append({"role": "user", "content": message})
137
  yield history
138
-
139
  history.append({"role": "assistant", "content": ""})
140
  yield history
141
 
142
- messages = []
143
- for msg in history[:-1]:
144
- content = msg["content"] if isinstance(msg["content"], str) else str(msg["content"])
145
- messages.append({"role": msg["role"], "content": content})
146
 
147
  try:
148
- stream = client.chat.completions.create(
149
- model=current_model,
150
- messages=messages,
151
- temperature=0.7,
152
- top_p=0.8,
153
- max_tokens=4096,
154
- stream=True
155
- )
156
  for chunk in stream:
157
  if chunk.choices and chunk.choices[0].delta.content is not None:
158
  history[-1]["content"] += chunk.choices[0].delta.content
159
  yield history
160
  except Exception as e:
161
- history[-1]["content"] = f"❌ Error: {str(e)}\nTry switching models."
162
  yield history
163
 
164
- # ================== GUI (dropdown now auto-loads) ==================
165
  with gr.Blocks(title="NVIDIA AI Chat") as demo:
166
  gr.Markdown("# 🟒 NVIDIA AI Chat\n**Multiple chats β€’ Mobile optimized β€’ Auto-save**")
167
  gr.Markdown("Powered By AIπŸ€– β€’ Edited/Compiled By tr0ecolor")
168
 
169
  with gr.Accordion("πŸ“– **How to Use the Interface (Step-by-Step)**", open=True):
170
  gr.Markdown("""
171
- **Step 1: Login / Register**
172
- Enter username + password β†’ click **Register** or **Login**. Fields auto-clear.
173
-
174
- **Step 2: Load Model**
175
- Pick model β†’ **Load & Test Model**.
176
-
177
- **Step 3: Manage Chats**
178
- β€’ **βž• New Chat**
179
- β€’ **Switch Chat** dropdown β†’ click any chat = loads instantly
180
- β€’ **πŸ—‘οΈ Delete Current**
181
-
182
- **Step 4: Chat!**
183
- Type β†’ Enter or **➀**. Message box clears automatically. All chats saved forever.
184
  """)
185
 
186
  current_model = gr.State(None)
@@ -195,23 +130,11 @@ Type β†’ Enter or **➀**. Message box clears automatically. All chats saved for
195
  gr.Markdown("### πŸ“‹ Your Chats")
196
  new_chat_btn = gr.Button("βž• New Chat", variant="primary", size="large")
197
  delete_chat_btn = gr.Button("πŸ—‘οΈ Delete Current", variant="stop", size="large")
198
-
199
- chat_dropdown = gr.Dropdown(
200
- choices=[],
201
- label="Switch Chat",
202
- interactive=True,
203
- value=None
204
- )
205
 
206
  with gr.Column(scale=4):
207
  with gr.Row():
208
- model_dropdown = gr.Dropdown(
209
- choices=available_models,
210
- value=default_model,
211
- label="🎯 Select Model",
212
- interactive=True,
213
- scale=4
214
- )
215
  load_btn = gr.Button("Load & Test Model", variant="primary", scale=1)
216
 
217
  chatbot = gr.Chatbot(height=480, buttons=["copy", "copy_all"])
@@ -227,67 +150,44 @@ Type β†’ Enter or **➀**. Message box clears automatically. All chats saved for
227
  login_btn = gr.Button("Login", variant="primary")
228
  logout_btn = gr.Button("Logout", visible=False, variant="stop")
229
 
 
 
 
 
 
 
 
230
  auth_globals_state = gr.State(auth_globals)
231
 
232
- # ================== EVENT HANDLERS ==================
233
- register_btn.click(
234
- register_user,
235
- [username_input, password_input, logged_in_username, auth_globals_state],
236
- [user_chats_state, current_chat_title, chatbot, chat_dropdown, logged_in_username, status_label, logout_btn]
237
- ).then(lambda: ("", ""), None, [username_input, password_input])
238
-
239
- login_btn.click(
240
- login_user,
241
- [username_input, password_input, logged_in_username, auth_globals_state],
242
- [user_chats_state, current_chat_title, chatbot, chat_dropdown, logged_in_username, status_label, logout_btn]
243
- ).then(lambda: ("", ""), None, [username_input, password_input])
244
-
245
- logout_btn.click(
246
- logout_user,
247
- [],
248
- [user_chats_state, current_chat_title, chatbot, chat_dropdown, logged_in_username, status_label, logout_btn]
249
- )
250
 
251
  load_btn.click(load_model, model_dropdown, [current_model, status_label])
252
 
253
- new_chat_btn.click(
254
- create_new_chat,
255
- [user_chats_state, logged_in_username],
256
- [user_chats_state, current_chat_title, chatbot, chat_dropdown, status_label]
257
- )
258
-
259
- delete_chat_btn.click(
260
- delete_current_chat,
261
- [current_chat_title, user_chats_state, logged_in_username],
262
- [user_chats_state, current_chat_title, chatbot, chat_dropdown, status_label]
263
- )
264
-
265
- # Auto-load when you click any chat in dropdown
266
- chat_dropdown.change(
267
- switch_chat,
268
- [chat_dropdown, user_chats_state],
269
- [chatbot, current_chat_title]
270
- )
271
-
272
- # Send message β†’ stream + auto-save after finish
273
- msg.submit(
274
- chat_stream,
275
- [msg, chatbot, current_model, logged_in_username, user_chats_state, current_chat_title],
276
- chatbot
277
- ).then(
278
- update_chats_after_response,
279
- [user_chats_state, current_chat_title, chatbot, logged_in_username],
280
- user_chats_state
281
- ).then(lambda: "", None, msg)
282
-
283
- send_btn.click(
284
- chat_stream,
285
- [msg, chatbot, current_model, logged_in_username, user_chats_state, current_chat_title],
286
- chatbot
287
- ).then(
288
- update_chats_after_response,
289
- [user_chats_state, current_chat_title, chatbot, logged_in_username],
290
- user_chats_state
291
- ).then(lambda: "", None, msg)
292
 
293
  demo.launch(theme=gr.themes.Soft(), ssr_mode=False, show_error=True)
 
1
  import os
2
  from openai import OpenAI
3
  import gradio as gr
4
+ from auth import register_user, login_user, logout_user, save_chats, delete_account
5
 
6
  # ================== YOUR SETTINGS ==================
7
  API_KEY = os.environ.get("API_KEY")
 
11
  if not HF_TOKEN:
12
  raise ValueError("HF_TOKEN secret not set in HF Spaces settings!")
13
 
14
+ client = OpenAI(base_url="https://integrate.api.nvidia.com/v1", api_key=API_KEY)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ auth_globals = {"hf_token": HF_TOKEN, "user_repo": USER_DATA_REPO}
17
+
18
+ model_choices = ["meta/llama-3.1-405b-instruct","meta/llama-3.1-70b-instruct","meta/llama-3.1-8b-instruct","mistralai/mistral-large-2-instruct","mistralai/mixtral-8x22b-instruct","mistralai/mistral-nemo-instruct-2407","qwen/qwen-2.5-72b-instruct","google/gemma-2-27b-it","google/gemma-2-9b-it","google/codegemma-7b-it"]
19
+ deprecated_models = ["qwen/qwen3-coder-480b-a35b-instruct","qwen/qwen2.5-coder-32b-instruct"]
20
  model_choices = [m for m in model_choices if m not in deprecated_models]
21
  default_model = model_choices[0] if model_choices else None
22
 
23
  def is_model_available(model_id):
24
  try:
25
+ test_completion = client.chat.completions.create(model=model_id, messages=[{"role": "user", "content": "Say 'test'"}], max_tokens=5, stream=False)
 
 
 
 
 
26
  return test_completion.choices[0].message.content is not None
27
  except Exception:
28
  return False
29
 
30
+ available_models = [model for model in model_choices if is_model_available(model)] or [default_model]
 
 
31
  default_model = available_models[0] if available_models else None
32
 
33
  def load_model(selected_model):
34
  try:
35
+ test_completion = client.chat.completions.create(model=selected_model, messages=[{"role": "user", "content": "Say 'test'"}], max_tokens=5, stream=False)
 
 
 
 
 
36
  if test_completion.choices[0].message.content:
37
  return selected_model, f"βœ… Loaded & Ready: {selected_model}"
38
  except Exception as e:
39
  return None, f"❌ Unavailable: {str(e)}\nTry another model."
40
 
 
41
  def create_new_chat(user_chats, logged_in_username):
42
+ if not logged_in_username: return user_chats, None, [], gr.update(choices=[], value=None), "⚠️ Please login first!"
 
43
  new_num = len(user_chats) + 1
44
  new_title = f"Chat {new_num}"
45
  new_chat = {"title": new_title, "history": []}
 
48
  return new_user_chats, new_title, [], gr.update(choices=choices, value=new_title), f"βœ… New chat created: {new_title}"
49
 
50
  def switch_chat(selected_title, user_chats):
51
+ if not selected_title or not user_chats: return [], None
 
52
  for chat in user_chats:
53
  if chat["title"] == selected_title:
54
  return chat["history"], selected_title
55
  return [], None
56
 
57
  def delete_current_chat(current_title, user_chats, logged_in_username):
58
+ if not logged_in_username or not current_title: return user_chats, current_title, [], gr.update(), "⚠️ Nothing to delete!"
 
59
  new_user_chats = [chat for chat in user_chats if chat["title"] != current_title]
60
  choices = [chat["title"] for chat in new_user_chats]
61
  if new_user_chats:
 
66
  new_history = []
67
  return new_user_chats, new_title, new_history, gr.update(choices=choices, value=new_title), f"πŸ—‘οΈ Deleted {current_title}"
68
 
 
69
  def update_chats_after_response(user_chats, current_chat_title, final_history, logged_in_username):
70
+ if not logged_in_username or not current_chat_title or not user_chats: return user_chats
 
71
  for chat in user_chats:
72
  if chat["title"] == current_chat_title:
73
+ chat["history"] = final_history[:]
74
  break
75
  save_chats(logged_in_username, user_chats, auth_globals)
76
  return user_chats
77
 
 
78
  def chat_stream(message, history, current_model, logged_in_username, user_chats, current_chat_title):
79
  history = history or []
80
+ if not logged_in_username:
 
81
  history.append({"role": "assistant", "content": "⚠️ Please login first!"})
82
+ yield history; return
83
+ if not current_model:
 
84
  history.append({"role": "assistant", "content": "⚠️ Load a model first!"})
85
+ yield history; return
86
+ if not current_chat_title:
 
87
  history.append({"role": "assistant", "content": "⚠️ Select or create a chat first!"})
88
+ yield history; return
 
89
 
 
90
  history.append({"role": "user", "content": message})
91
  yield history
 
92
  history.append({"role": "assistant", "content": ""})
93
  yield history
94
 
95
+ messages = [{"role": msg["role"], "content": str(msg["content"])} for msg in history[:-1]]
 
 
 
96
 
97
  try:
98
+ stream = client.chat.completions.create(model=current_model, messages=messages, temperature=0.7, top_p=0.8, max_tokens=4096, stream=True)
 
 
 
 
 
 
 
99
  for chunk in stream:
100
  if chunk.choices and chunk.choices[0].delta.content is not None:
101
  history[-1]["content"] += chunk.choices[0].delta.content
102
  yield history
103
  except Exception as e:
104
+ history[-1]["content"] = f"❌ Error: {str(e)}"
105
  yield history
106
 
107
+ # ================== GUI ==================
108
  with gr.Blocks(title="NVIDIA AI Chat") as demo:
109
  gr.Markdown("# 🟒 NVIDIA AI Chat\n**Multiple chats β€’ Mobile optimized β€’ Auto-save**")
110
  gr.Markdown("Powered By AIπŸ€– β€’ Edited/Compiled By tr0ecolor")
111
 
112
  with gr.Accordion("πŸ“– **How to Use the Interface (Step-by-Step)**", open=True):
113
  gr.Markdown("""
114
+ **Step 1:** Login / Register (fields auto-clear)
115
+ **Step 2:** Load a model
116
+ **Step 3:** Manage chats (New / Switch / Delete current)
117
+ **Step 4:** Chat! Everything auto-saves
118
+ **Danger Zone (bottom):** Delete your entire account permanently
 
 
 
 
 
 
 
 
119
  """)
120
 
121
  current_model = gr.State(None)
 
130
  gr.Markdown("### πŸ“‹ Your Chats")
131
  new_chat_btn = gr.Button("βž• New Chat", variant="primary", size="large")
132
  delete_chat_btn = gr.Button("πŸ—‘οΈ Delete Current", variant="stop", size="large")
133
+ chat_dropdown = gr.Dropdown(choices=[], label="Switch Chat", interactive=True, value=None)
 
 
 
 
 
 
134
 
135
  with gr.Column(scale=4):
136
  with gr.Row():
137
+ model_dropdown = gr.Dropdown(choices=available_models, value=default_model, label="🎯 Select Model", interactive=True, scale=4)
 
 
 
 
 
 
138
  load_btn = gr.Button("Load & Test Model", variant="primary", scale=1)
139
 
140
  chatbot = gr.Chatbot(height=480, buttons=["copy", "copy_all"])
 
150
  login_btn = gr.Button("Login", variant="primary")
151
  logout_btn = gr.Button("Logout", visible=False, variant="stop")
152
 
153
+ # Danger Zone
154
+ with gr.Accordion("⚠️ Danger Zone - Permanent Actions", open=False):
155
+ delete_acc_btn = gr.Button("πŸ—‘οΈ Delete My Account", variant="stop", size="large", visible=False)
156
+ with gr.Row(visible=False) as confirm_row:
157
+ confirm_username_input = gr.Textbox(label="Type your exact username to confirm", placeholder="your_username")
158
+ confirm_delete_btn = gr.Button("YES, DELETE ACCOUNT FOREVER", variant="stop")
159
+
160
  auth_globals_state = gr.State(auth_globals)
161
 
162
+ # Event Handlers
163
+ register_btn.click(register_user, [username_input, password_input, logged_in_username, auth_globals_state],
164
+ [user_chats_state, current_chat_title, chatbot, chat_dropdown, logged_in_username, status_label, logout_btn, delete_acc_btn, confirm_row]).then(lambda: ("",""), None, [username_input, password_input])
165
+
166
+ login_btn.click(login_user, [username_input, password_input, logged_in_username, auth_globals_state],
167
+ [user_chats_state, current_chat_title, chatbot, chat_dropdown, logged_in_username, status_label, logout_btn, delete_acc_btn, confirm_row]).then(lambda: ("",""), None, [username_input, password_input])
168
+
169
+ logout_btn.click(logout_user, [], [user_chats_state, current_chat_title, chatbot, chat_dropdown, logged_in_username, status_label, logout_btn, delete_acc_btn, confirm_row])
170
+
171
+ delete_acc_btn.click(lambda: gr.update(visible=True), None, confirm_row)
172
+
173
+ confirm_delete_btn.click(delete_account, [confirm_username_input, logged_in_username, auth_globals_state],
174
+ [status_label, logged_in_username, logout_btn, delete_acc_btn, confirm_row, user_chats_state, current_chat_title, chatbot, chat_dropdown])
 
 
 
 
 
175
 
176
  load_btn.click(load_model, model_dropdown, [current_model, status_label])
177
 
178
+ new_chat_btn.click(create_new_chat, [user_chats_state, logged_in_username], [user_chats_state, current_chat_title, chatbot, chat_dropdown, status_label])
179
+
180
+ delete_chat_btn.click(delete_current_chat, [current_chat_title, user_chats_state, logged_in_username],
181
+ [user_chats_state, current_chat_title, chatbot, chat_dropdown, status_label])
182
+
183
+ chat_dropdown.change(switch_chat, [chat_dropdown, user_chats_state], [chatbot, current_chat_title])
184
+
185
+ msg.submit(chat_stream, [msg, chatbot, current_model, logged_in_username, user_chats_state, current_chat_title], chatbot)\
186
+ .then(update_chats_after_response, [user_chats_state, current_chat_title, chatbot, logged_in_username], user_chats_state)\
187
+ .then(lambda: "", None, msg)
188
+
189
+ send_btn.click(chat_stream, [msg, chatbot, current_model, logged_in_username, user_chats_state, current_chat_title], chatbot)\
190
+ .then(update_chats_after_response, [user_chats_state, current_chat_title, chatbot, logged_in_username], user_chats_state)\
191
+ .then(lambda: "", None, msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
  demo.launch(theme=gr.themes.Soft(), ssr_mode=False, show_error=True)