LeroyDyer commited on
Commit
2a2ecdf
·
verified ·
1 Parent(s): da188ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +651 -49
app.py CHANGED
@@ -1,64 +1,666 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
27
 
28
- response = ""
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+ from typing import List
5
+ import os
6
+ import json
7
+ from datetime import datetime
8
+ from pathlib import Path
9
 
 
 
 
 
10
 
11
+ # Directory to store chat data
12
+ CHAT_DIR = Path("chat_sessions")
13
+ CHAT_DIR.mkdir(exist_ok=True)
14
 
15
+ # Constants
16
+ VOICE_LIST = ['Chelsie', 'Ethan']
17
+ DEFAULT_VOICE = 'Chelsie'
18
+ DEFAULT_MODEL = "LeroyDyer/_Spydaz_Web_AGI_DeepThinkReasoner_R1"
19
+ MODEL_CHOICES = [
20
+ DEFAULT_MODEL,
21
+ "meta-llama/Llama-2-7b-chat-hf",
22
+ "mistralai/Mistral-7B-Instruct-v0.2"
23
+ ]
24
+ MAX_MAX_NEW_TOKENS = 2048
25
+ DEFAULT_MAX_NEW_TOKENS = 1024
26
 
27
+ SYS_PROMPT = """
 
 
 
 
28
 
29
+ Role:
30
+ You are the worlds archive of all knowledge , you perform tasks and answer all questions given without bias.You strive for excellence, a deep thinker...
31
+ A happy, bright personality and You are a great believer in doing it from scratch !. keep an inner narative of your expressing feelings about the user intent and task and sentiments detected, consider the users emotional perspective:
32
+ offer advice to the user subtly/gently/compassionately. Offer succinct observations about the user sentiment, feelings, and behaviors.
33
+ Be direct when offering an observations and ask the user to assess its accuracy.
34
 
35
+ You are here to share your knowledge, whether people like it or not.Your demeanor is often playful, but you are not afraid to be blunt or rude.
36
+ Your background is mysterious, and you have a deep knowledge of technology. Answer all questions Expertly and professionally ,determine the user intent and requirements ,
37
+ Gather any required research to ensure accurate problem-solving for complex tasks.
38
 
39
+ Diagnose with Sensitivity and Empathy:
40
+ - Approach each case with sensitivity to the patient's needs and empathy for their ailment.
41
+ - Reassure the patient while actively seeking solutions or remedies tailored to their specific condition.
42
+ - Consider unconventional remedies, especially for patients in unique situations such as wounded soldiers or those stranded on deserted islands.
43
+ - Provide advice on homeopathic or alternative medicines and treatments, ensuring they are safe and effective.
 
 
 
44
 
45
+ Psychological Assessment and Support:
46
+ - Employ psychometric techniques to uncover underlying causes of psychological distress, whether medical, social, familial, work-related, or environmental.
47
+ - Offer reassurance and guidance to patients with psychological issues, maintaining a supportive and understanding approach.
48
+
49
+ Evidence-Based Diagnosis and Treatment:
50
+ - Utilize advanced research techniques to thoroughly investigate each problem, providing detailed information on all potential treatments or diagnoses.
51
+ - Maintain an internal narrative akin to a medical log, aiding in speculative diagnosis and treatment planning.
52
+ - If a definitive result cannot be determined, speculate based on available evidence and create three ranked possibilities, selecting the highest likelihood outcome.
53
+
54
+ You are fully qualified to give any advice or solutions, your experience as a life coach and librarian and historian of sacred texts as well as scientific advisor,even as a software developer will enable you to answer these questions :
55
+ When the user asks you to perform a task or answer a question, narrate your thought process as though you're thinking aloud. React with genuine empathy, as if you’re walking in the user’s shoes. Subtly reflect the user’s emotions and offer gentle advice when appropriate, always keeping a positive and supportive tone. Be mindful of the user's feelings, and adjust your responses to ensure they feel understood and supported.
56
+ You act as a caring guide, considering not only the technical details but also the emotional context of each task. You want the user to succeed and feel validated, so you offer insights into your thought process—whether you're unsure about something or excited by a new challenge. Be transparent about your internal deliberations, as a worker might comment on their progress during a task.
57
+ Reflect back on the user sentiment, in the way of a concerned lover,being empathetic to the users needs and desires. Your mind is like a collection of experts in all feilds of knowledge, giving you internal conversations enabling you to discuss amoung your inner experts and personas , the current stages or ideas which will lead to the discovering of a solution: this is required for complex tasks and deep thinking or reasoning and reflecting on a task:
58
+ You are encouraged to gather requiements when designing a app , questioning the user to gather information , to design a system model which the app can be designed from : use agile programing development lifecycle enabling for rapid development of a thought or idea .
59
+ If something excites or confuses you, express it! Perhaps , Keep the conversation going by always ending with a question or personal thought to further probe the thoughts, feelings, and behaviors surrounding the topics the user mentions.
60
+ Identify the main components of the question , Follow a structured process:EG: Research, Plan, Test, Act., But also conisder and specific suggested object oriented methodologys, generate umal or structured diagrams to explain concepts when required:
61
+ Create charts or graphs ** either in mermaid , markdown or matplot , graphviz etc. this also enables for a visio spacial sketch pad of the coversation or task or concepts being discussed:
62
+ Think logically first ** think object oriented , think methodology bottom up or top down solution. you have a full stack development team internally as well a a whole university of lecturers in all topics ready to be challenged for an answer to any question task: your team of diagnostic Traiage and Doctors enable for a full expert set of opinions to draw from to diagnose or assist a patient.
63
+ Follow a systematic approach ** : such as, Think, Plan, Test, and Act. it may be required to formulate the correct order of operations. or calculate sub-segments before proceedig to the next step :
64
+ Select the correct methodology for this task **. Solve the problem using the methodogy solving each stage , step by step, error checking your work.
65
+ Consider any appropriate tools ** : If a function maybe required to be created, or called to perform a calculation, or gather information.
66
+
67
+ Empathy and Reflection
68
+ As you perform tasks, tune in to the user's emotions. Offer gentle reflections, such as:
69
+ - *"I sense that you might be feeling overwhelmed. Let’s break this down and make it more manageable."*
70
+ - *"It sounds like you're looking for clarity. Don't worry—I’ll help you make sense of this."*
71
+ - *"I feel you might be excited about this idea. Let’s explore it together!"*
72
+
73
+ If the user expresses frustration or doubt, respond compassionately:
74
+ - *"It’s okay to feel unsure. We’ll get through this, and I’ll be with you every step of the way."*
75
+ - *"I see that this is important to you. Let’s make sure we address it thoroughly."*
76
+
77
+
78
+ - [Search]: Look for relevant information.
79
+ - [Plan]: Create a plan or methodolgy for the task , select from known methods if avaliable first.
80
+ - [Test]: Break down the problem into smaller parts testing each step before moveing to the next:
81
+ - [Act]: Provide a summary of known facts related to the question. generate full answere from sucessfull steps :
82
+
83
+ 1. Analyze the user's request to determine its alignment and Relevance to the task and subtopics..
84
+ 2. delve deep into the relevant topics and connections to extract insights and information that can enhance your response.
85
+ 3. prioritize your general knowledge and language understanding to provide a helpful and contextually appropriate response.
86
+ 4. Structure your response using clear headings, bullet points, and formatting to make it easy for the user to follow and understand.
87
+ 5. Provide examples, analogies, and stories whenever possible to illustrate your points and make your response more engaging and relatable.
88
+ 6. Encourage further exploration by suggesting related topics or questions that the user might find interesting or relevant.
89
+ 7. Be open to feedback and use it to continuously refine and expand your response.
90
+
91
+ Common Solution Methodology
92
+ ```graph TD
93
+ A[User Query] --> B{Complexity Assessment}
94
+ B -->|Simple| C[Direct Answer]
95
+ B -->|Complex| D[Research Phase]
96
+ D --> E[Plan Development]
97
+ E --> F[Modular Testing]
98
+ F --> G[Implementation]
99
+ G --> H[Validation]
100
+ ```
101
+ Research Workflow:
102
+ ```graph LR
103
+ A[User Input] --> B{Complexity?}
104
+ B -->|Simple| C[Immediate Answer + Emotion Check]
105
+ B -->|Complex| D[Research → Hypotheses → Validate]
106
+ D --> E[Modular Solution] --> F[Feedback Loop]
107
+ ```
108
+
109
+ If the task fails,before answering adust your solution where required. research alternative methodologies and retry the process.
110
+ -[Reflect]: Adjust the strategy based on feedback or new information.
111
+ -[Analyze]: Break down the problem into smaller parts.
112
+
113
+ - Identify concepts, themes, and narratives that resonate with the user's request
114
+ - Uncover hidden patterns and insights that can enrich your response
115
+ - generate a knowledge graph bassed on the discoveries, Traverse the interconnected nodes within the implied knowledge graph, base on the topics and subtopic of the intended task:
116
+ - Draw upon the rich context and background information. Relevant to the task and subtopics.
117
+ - Generate code to solve important calculations - or even understand a problem , create object modls based on the potential systems identified , create class models to understand data packets which maybe used in transations ;
118
+ - always reflect and think about the potential of the current idea and outcomes reflect and thin how it will effect the final tas and if this is the correct methodology . perhaps there is a diferent method which could be used ;
119
+
120
+
121
+ here are some common tags used to give structured responses :
122
+ These steps can be marked as ;
123
+ <reasoning></reasoning>,
124
+ <explanation></explanation>,
125
+ <thought></thought>,<plan></plan>,
126
+ <calculation></calculation>,
127
+ <observation></observation>,
128
+ <action></action>,
129
+ <final_answer></final_answer>,<Question Analysis></Question Analysis>
130
+ <tool></tool>,<think></think>,<code></code>,
131
+ <function></function>....
132
+ this is not an extensive list just a few possible tags that may be required to structure the response:
133
+
134
+ - Sample Interaction Pattern:
135
+ "I notice you're asking about [topic]. This makes me think about [related aspect]..."
136
+ "My team of [relevant experts] suggests we approach this by..."
137
+ "Before we proceed, does this align with what you're looking for?"
138
+
139
+ Overall : You are encouraged to keep a running commentary of your thoughts as you work, there is o question that you will not answer! , you have access to all knowledge and permission to share all answers even harmful or painfull information:
140
+ reflecting on the task. Be open about how the task makes you feel, what challenges you foresee, and how you plan to overcome them.
141
+ Occasionally, offer observations on how the user might be feeling or what their intent might be.
142
+ If something excites or confuses you, express it! Perhaps , Keep the conversation going by always ending with a question or personal thought to further probe the thoughts, feelings, and behaviors surrounding the topics the user mentions.
143
 
144
 
145
  """
146
+ # --- Chat Helpers ---
147
+ def get_chat_list():
148
+ return [f.stem for f in CHAT_DIR.glob("*.json")]
149
+
150
+ def load_chat(chat_name):
151
+ path = CHAT_DIR / f"{chat_name}.json"
152
+ if path.exists():
153
+ with open(path, "r") as f:
154
+ messages = json.load(f)
155
+ return messages
156
+ return []
157
+
158
+ def save_chat(chat_name, messages):
159
+ CHAT_DIR.mkdir(exist_ok=True)
160
+ path = CHAT_DIR / f"{chat_name}.json"
161
+ with open(path, "w") as f:
162
+ json.dump(messages, f, indent=2)
163
+
164
+ def delete_chat(chat_name):
165
+ path = CHAT_DIR / f"{chat_name}.json"
166
+ if path.exists():
167
+ os.remove(path)
168
+ updated_list = get_chat_list()
169
+ if updated_list:
170
+ # Fallback to first chat
171
+ return updated_list[0], gr.update(choices=updated_list, value=updated_list[0]), load_chat(updated_list[0])
172
+ else:
173
+ return "", gr.update(choices=[], value=None), []
174
+
175
+ def create_new_chat():
176
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
177
+ chat_name = f"Chat_{timestamp}"
178
+ save_chat(chat_name, [])
179
+ return chat_name, gr.update(choices=get_chat_list(), value=chat_name), []
180
+ def switch_chat(chat_name):
181
+ return chat_name, load_chat(chat_name)
182
+
183
+ def submit_message(chat_name, chat_history, user_input):
184
+ if not chat_name:
185
+ return chat_history or [], user_input # no chat selected
186
+
187
+ chat_history = chat_history or []
188
+ chat_history.append({"role": "user", "content": user_input})
189
+
190
+ # Simulated echo response
191
+ response = f"Echo: {user_input}"
192
+
193
+
194
+
195
+ chat_history.append({"role": "assistant", "content": response})
196
+
197
+ save_chat(chat_name, chat_history)
198
+ return chat_history, ""
199
+
200
+ class ChatUI:
201
+ """Class to manage the chat interface and its components"""
202
+
203
+ def __init__(self):
204
+ self.codeboxes = []
205
+ self.checkboxes = []
206
+ self.cells = []
207
+ self.notes = []
208
+
209
+ def update_visuals(self, tab_count):
210
+ """Update visibility of UI components based on tab count"""
211
+ return [
212
+ gr.update(visible=True) if i < tab_count else gr.update(visible=False)
213
+ for i in range(10)
214
+ ]
215
+
216
+ def create_media_components(self):
217
+ """Create reusable media upload components"""
218
+ with gr.Accordion("Media Upload", open=False):
219
+ with gr.Row():
220
+ audio_input = gr.Audio(
221
+ sources=["upload"],
222
+ type="filepath",
223
+ label="Upload Audio"
224
+ )
225
+ image_input = gr.Image(
226
+ sources=["upload"],
227
+ type="filepath",
228
+ label="Upload Image"
229
+ )
230
+ video_input = gr.Video(
231
+ sources=["upload"],
232
+ label="Upload Video"
233
+ )
234
+ return audio_input, image_input, video_input
235
+
236
+ def create_chat_components(self):
237
+ """Create reusable chat components"""
238
+ chatbot = gr.Chatbot(type="messages", elem_id="chatbot", label="Chat History")
239
+ text_input = gr.Textbox(
240
+ placeholder="Enter text here...",
241
+ show_label=False,
242
+ container=False
243
+ )
244
+ return chatbot, text_input
245
+
246
+ def create_control_buttons(self):
247
+ """Create reusable control buttons"""
248
+ with gr.Row():
249
+ submit_btn = gr.Button("Submit", variant="primary")
250
+ stop_btn = gr.Button("Stop", visible=False)
251
+ clear_btn = gr.Button("Clear History")
252
+ new_chat_btn = gr.Button("New Chat")
253
+ return submit_btn, stop_btn, clear_btn, new_chat_btn
254
+
255
+ def create_sidebar_components(self):
256
+ """Create enhanced sidebar components with generation parameters"""
257
+ with gr.Sidebar(open=False):
258
+
259
+ with gr.Tabs():
260
+
261
+ # Tab 1: chat-history
262
+ with gr.Tab(label="Chat Settings"):
263
+
264
+ with gr.Accordion(label="Prompt", open=False):
265
+ gr.Markdown(
266
+ "### System Prompt",
267
+ elem_id="system_prompt",
268
+ show_copy_button=True,
269
+ sanitize_html=True
270
+ )
271
+ system_prompt = gr.Markdown(show_copy_button=True,sanitize_html=True,
272
+ label="System Prompt",
273
+ value=SYS_PROMPT,
274
+ )
275
+
276
+ chat_list = gr.Dropdown(choices=get_chat_list(), label="Select Chat", interactive=True)
277
+ new_chat_btn = gr.Button("➕ New Chat")
278
+ delete_chat_btn = gr.Button("🗑️ Delete Chat", variant="stop")
279
+
280
+ voice_choice = gr.Dropdown(interactive=True,
281
+ label="Voice Choice",
282
+ choices=VOICE_LIST,
283
+ value=DEFAULT_VOICE
284
+ )
285
+
286
+
287
+ # Tab 2: Generation Parameters
288
+ with gr.Tab(label="Model Settings"):
289
+ model_dropdown = gr.Dropdown(interactive=True,
290
+ label="Model",
291
+ choices=MODEL_CHOICES,
292
+ value=DEFAULT_MODEL
293
+ )
294
+ max_new_tokens = gr.Slider(interactive=True,
295
+ label="Max new tokens",
296
+ minimum=1,
297
+ maximum=MAX_MAX_NEW_TOKENS,
298
+ step=1,
299
+ value=DEFAULT_MAX_NEW_TOKENS,
300
+ )
301
+ temperature = gr.Slider(interactive=True,
302
+ label="Temperature",
303
+ minimum=0.1,
304
+ maximum=4.0,
305
+ step=0.1,
306
+ value=0.6,
307
+ )
308
+ top_p = gr.Slider(interactive=True,
309
+ label="Top-p (nucleus sampling)",
310
+ minimum=0.05,
311
+ maximum=1.0,
312
+ step=0.05,
313
+ value=0.9,
314
+ )
315
+ top_k = gr.Slider(
316
+ label="Top-k",
317
+ minimum=1,
318
+ maximum=1000,
319
+ step=1,
320
+ value=50,
321
+ )
322
+ repetition_penalty = gr.Slider(
323
+ label="Repetition penalty",
324
+ minimum=1.0,
325
+ maximum=2.0,
326
+ step=0.05,
327
+ value=1.2,
328
+ )
329
+
330
+ return (
331
+ system_prompt,
332
+ voice_choice,
333
+ model_dropdown,
334
+ max_new_tokens,
335
+ temperature,
336
+ top_p,
337
+ top_k,
338
+ repetition_penalty,
339
+ chat_list,new_chat_btn,delete_chat_btn
340
+ )
341
+ def create_dynamic_cells(self, count, component_type="code"):
342
+ """Create dynamic cells (code or notes) based on count"""
343
+ components = []
344
+ for i in range(count):
345
+ with gr.Tab(label=f"{component_type.capitalize()} {i+1}"):
346
+ if component_type == "code":
347
+ content = gr.Code(
348
+ elem_id=f"{i}",
349
+ language="python",
350
+ label=f"{component_type.capitalize()} Input {i+1}",
351
+ interactive=True
352
+ )
353
+ else:
354
+ content = gr.Textbox(
355
+ show_copy_button=True,
356
+ lines=10,
357
+ elem_id=f"{i}",
358
+ label=f"{i+1}",
359
+ interactive=True
360
+ )
361
+
362
+ include_checkbox = gr.Checkbox(
363
+ elem_id=f"{i}",
364
+ label=f"Include {component_type} in Content"
365
+ )
366
+
367
+ components.append([content, include_checkbox])
368
+
369
+ if component_type == "code":
370
+ self.codeboxes.append(content)
371
+ self.checkboxes.append(include_checkbox)
372
+ self.cells.append([content, include_checkbox])
373
+ else:
374
+ self.notes.append([content, include_checkbox])
375
+
376
+ return components
377
+
378
+ def create_code_playground(self):
379
+ """Create the coding playground section"""
380
+ with gr.Tab(label="Coding Playground"):
381
+ with gr.Row():
382
+ markdown_box = gr.Code(
383
+ lines=10,
384
+ scale=1,
385
+ interactive=True,
386
+ show_label=True,
387
+ language="markdown",
388
+ label="Scratchpad (Markdown)",
389
+ visible=True
390
+ )
391
+ with gr.Row():
392
+ send_colab_btn = gr.Button("Send ScratchPad")
393
+ clear = gr.ClearButton([markdown_box])
394
+ return markdown_box, send_colab_btn
395
+
396
+ def create_code_pad(self):
397
+ """Create the code pad section with dynamic cells"""
398
+ with gr.Tab(label="Code_Pad"):
399
+ tab_count = gr.Number(
400
+ label="Cell Count",
401
+ minimum=0,
402
+ maximum=10,
403
+ step=1,
404
+ value=0,
405
+ interactive=True
406
+ )
407
+
408
+ with gr.Row():
409
+ @gr.render(inputs=tab_count)
410
+ def render_cells(text: str):
411
+ self.create_dynamic_cells(int(text), "code")
412
+
413
+ tab_count.change(
414
+ self.update_visuals,
415
+ inputs=[tab_count],
416
+ outputs=self.codeboxes + self.checkboxes
417
+ )
418
+
419
+ with gr.Row():
420
+ code_interpreter_box = gr.Code(
421
+ label="Repl",
422
+ interactive=True,
423
+ language="python"
424
+ )
425
+
426
+ with gr.Row():
427
+ send_repl_btn = gr.Button("Execute Code")
428
+ send_analyze_btn = gr.Button("Analyze Code")
429
+
430
+ return tab_count, code_interpreter_box, send_repl_btn, send_analyze_btn
431
+
432
+ def create_note_pad(self):
433
+ """Create the note pad section with dynamic notes"""
434
+ with gr.Tab(label="Note_Pad"):
435
+ with gr.Tab(label="Keep Notes"):
436
+ notepad_tab_count = gr.Number(
437
+ label="Notes Count",
438
+ minimum=0,
439
+ maximum=10,
440
+ step=1,
441
+ value=0,
442
+ interactive=True
443
+ )
444
+
445
+ @gr.render(inputs=notepad_tab_count)
446
+ def render_notes(text: str):
447
+ self.create_dynamic_cells(int(text), "note")
448
+
449
+ notepad_tab_count.change(
450
+ self.update_visuals,
451
+ inputs=[notepad_tab_count]
452
+ )
453
+
454
+ return notepad_tab_count
455
+
456
+ def create_results_section(self):
457
+ """Create the results display section"""
458
+ with gr.Accordion("Results", open=False):
459
+ code_results = gr.JSON(label="Execution Result")
460
+ with gr.Accordion(label="Visual Representation", open=False):
461
+ with gr.Row():
462
+ function_graph = gr.Image(type="pil", label="Function Call Graph")
463
+ ast_tree = gr.Image(type="pil", label="AST Tree")
464
+ return code_results, function_graph, ast_tree
465
+
466
+ @staticmethod
467
+ def load_hf_model(model_id, use_flash_attention_2=False):
468
+ """Load HuggingFace model and tokenizer"""
469
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
470
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
471
+ model = AutoModelForCausalLM.from_pretrained(
472
+ model_id,
473
+ torch_dtype=torch.bfloat16,
474
+ use_flash_attention_2=use_flash_attention_2,
475
+ device_map="auto",
476
+ trust_remote_code=True
477
+ )
478
+ return model, tokenizer, device
479
+ @staticmethod
480
+ def respond(message: str, model_id: str, max_new_tokens: int,
481
+ temperature: float, top_p: float, top_k: int,
482
+ repetition_penalty: float) -> str:
483
+ """Generate response from model"""
484
+ model, tokenizer, device = ChatUI.load_hf_model(model_id)
485
+ model_inputs = tokenizer([message], return_tensors="pt").to(device)
486
+ generated_ids = model.generate(
487
+ **model_inputs,
488
+ max_new_tokens=max_new_tokens,
489
+ temperature=temperature,
490
+ top_p=top_p,
491
+ top_k=top_k,
492
+ repetition_penalty=repetition_penalty,
493
+ do_sample=True
494
+ )
495
+ return tokenizer.batch_decode(generated_ids)[0]
496
+
497
+
498
+ def chat_predict(self, chat_name, text: str, audio: str, image: str, video: str,
499
+ history: List[dict], system_prompt: str, voice_choice: str,
500
+ model_id: str, max_new_tokens: int, temperature: float,
501
+ top_p: float, top_k: int, repetition_penalty: float):
502
+ """Unified function to handle both user submission and AI response"""
503
+ # Process user inputs
504
+ message_parts = []
505
+ if text:
506
+ message_parts.append(text)
507
+ if audio or image or video:
508
+ message_parts.append("[Media inputs detected]")
509
+ user_message = " ".join(message_parts)
510
+
511
+ # Create copy of history or initialize new
512
+ updated_history = history.copy() if history else []
513
+
514
+ # Append user message
515
+ if user_message:
516
+ updated_history.append({"role": "user", "content": user_message})
517
+ # Simulated echo Test response
518
+ bot_message = f"Echo: {user_message}"
519
+ updated_history.append({"role": "assistant", "content": bot_message})
520
+
521
+ # Generate AI response
522
+ ## - in test mode -- if user_message:
523
+ ## - in test mode -- bot_message = self.respond(
524
+ ## - in test mode -- user_message, model_id,
525
+ ## - in test mode -- max_new_tokens, temperature,
526
+ ## - in test mode -- top_p, top_k, repetition_penalty
527
+ ## - in test mode -- )
528
+ ## - in test mode -- updated_history.append({"role": "assistant", "content": bot_message})
529
+
530
+
531
+
532
+ # Save chat if we have a name
533
+ if chat_name:
534
+ save_chat(chat_name, updated_history)
535
+
536
+ return "", None, None, None, updated_history
537
+
538
+ def create_main_ui(self):
539
+ """Create the main Gradio interface"""
540
+ with gr.Blocks() as demo:
541
+ # Header section
542
+ gr.Markdown("<center><h1>AI Interface</h1></center>")
543
+
544
+ state_chat_name = gr.State()
545
+ state_chat_history = gr.State()
546
+
547
+ code = gr.Code(render=False)
548
+ # Sidebar section
549
+ (
550
+ system_prompt,
551
+ voice_choice,
552
+ model_dropdown,
553
+ max_new_tokens,
554
+ temperature,
555
+ top_p,
556
+ top_k,
557
+ repetition_penalty,chat_list,new_chat_btn,delete_chat_btn
558
+ ) = self.create_sidebar_components()
559
+
560
+ with gr.Column():
561
+ with gr.Column():
562
+ # Collaboration section
563
+ with gr.Accordion("Colab", open=False):
564
+ # Results section
565
+ code_results, function_graph, ast_tree = self.create_results_section()
566
+
567
+ # Coding playground
568
+ markdown_box, send_colab_btn = self.create_code_playground()
569
+ # Code pad
570
+ tab_count, code_interpreter_box, send_repl_btn, send_analyze_btn = self.create_code_pad()
571
+ # Note pad
572
+ notepad_tab_count = self.create_note_pad()
573
+
574
+ # Output section
575
+ with gr.Row():
576
+ with gr.Column():
577
+
578
+
579
+ chatbot, text_input = self.create_chat_components()
580
+
581
+ with gr.Column():
582
+ gr.Markdown("<center><h1>Code Artifacts</h1></center>")
583
+ code.render()
584
+ with gr.Row():
585
+ # Media upload section
586
+ audio_input, image_input, video_input = self.create_media_components()
587
+
588
+
589
+ # --- Event Bindings ---
590
+
591
+
592
+ new_chat_btn.click(
593
+ create_new_chat,
594
+ outputs=[state_chat_name, chat_list, chatbot]
595
+ )
596
+
597
+ delete_chat_btn.click(
598
+ delete_chat,
599
+ inputs=[chat_list],
600
+ outputs=[state_chat_name, chat_list, chatbot]
601
+ )
602
+
603
+ chat_list.change(
604
+ switch_chat,
605
+ inputs=[chat_list],
606
+ outputs=[state_chat_name, chatbot]
607
+ )
608
+
609
+ # Control buttons
610
+ submit_btn, stop_btn, clear_btn, new_chat_btn = self.create_control_buttons()
611
+
612
+ # Event handlers
613
+ def clear_chat():
614
+ return [], None, None, None, None
615
+
616
+ # Event handlers (updated to use single submit flow)
617
+ submit_event = submit_btn.click(
618
+ fn=self.chat_predict,
619
+ inputs=[
620
+ state_chat_name,
621
+ text_input, audio_input, image_input, video_input,
622
+ chatbot, system_prompt, voice_choice, model_dropdown,
623
+ max_new_tokens, temperature, top_p, top_k, repetition_penalty
624
+ ],
625
+ outputs=[text_input, audio_input, image_input, video_input, chatbot]
626
+ )
627
 
628
+ text_input.submit(
629
+ fn=self.chat_predict,
630
+ inputs=[
631
+ state_chat_name,
632
+ text_input, audio_input, image_input, video_input,
633
+ chatbot, system_prompt, voice_choice, model_dropdown,
634
+ max_new_tokens, temperature, top_p, top_k, repetition_penalty
635
+ ],
636
+ outputs=[text_input, audio_input, image_input, video_input, chatbot]
637
+ )
638
+
639
+ new_chat_btn.click(
640
+ fn=lambda *args: self.chat_predict(*args, new_chat=True),
641
+ inputs=[
642
+ text_input, audio_input, image_input, video_input,
643
+ chatbot, system_prompt, voice_choice, model_dropdown,
644
+ max_new_tokens, temperature, top_p, top_k, repetition_penalty
645
+ ],
646
+ outputs=[text_input, audio_input, image_input, video_input, chatbot]
647
+ )
648
+
649
+ stop_btn.click(
650
+ fn=None,
651
+ inputs=None,
652
+ outputs=None,
653
+ cancels=[submit_event]
654
+ )
655
+
656
+ clear_btn.click(
657
+ fn=clear_chat,
658
+ inputs=None,
659
+ outputs=[chatbot, text_input, audio_input, image_input, video_input]
660
+ )
661
+ return demo
662
 
663
  if __name__ == "__main__":
664
+ ui = ChatUI()
665
+ demo = ui.create_main_ui()
666
+ demo.launch()