prashantmatlani commited on
Commit
642547d
·
1 Parent(s): fdc8455

ui change and auto-save conversation history

Browse files
Files changed (4) hide show
  1. app.py +45 -15
  2. app_earlier.py +55 -0
  3. storage.py +3 -7
  4. styles.css +10 -0
app.py CHANGED
@@ -6,27 +6,29 @@ The Interface Skeleton - The code sets up the navigation panel and the multimoda
6
  """
7
 
8
 
9
- import gradio as gr
10
- from core_logic import chat_function
11
- from storage import save_chat, load_history, get_chat_content
12
-
13
  import gradio as gr
14
  from core_logic import chat_function
15
  from storage import save_chat, load_history, get_chat_content
16
 
17
  with gr.Blocks() as demo:
 
18
  chat_id_state = gr.State("")
19
 
20
  with gr.Row():
 
21
  with gr.Column(scale=1, variant="secondary"):
22
  gr.Markdown("### 🛠️ Silicon Architect")
23
  new_btn = gr.Button("➕ New Chat", variant="primary")
 
 
24
  history_list = gr.Dataset(
25
  components=[gr.Textbox(visible=False)],
26
  label="Recent Conversations",
27
- samples=load_history()
 
28
  )
29
 
 
30
  with gr.Column(scale=4):
31
  chatbot = gr.Chatbot(show_label=False, height=700)
32
  chat_input = gr.MultimodalTextbox(
@@ -35,25 +37,53 @@ with gr.Blocks() as demo:
35
  show_label=False
36
  )
37
 
38
- def bot_response(message, history):
39
- # 1. Add User Message
 
40
  user_content = message["text"]
41
  history.append({"role": "user", "content": user_content})
42
-
43
- # 2. Add empty Assistant Message to be filled
44
  history.append({"role": "assistant", "content": ""})
45
 
46
- # 3. Stream the response
47
- # history[:-1] sends everything EXCEPT the empty assistant slot we just made
48
  for partial_resp in chat_function(message, history[:-1]):
49
  history[-1]["content"] = partial_resp
50
  yield history
51
 
52
- # Event Handlers
53
- chat_input.submit(bot_response, [chat_input, chatbot], [chatbot]).then(
54
- lambda h: save_chat(None, h), [chatbot], None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  )
56
 
57
- new_btn.click(lambda: ([], ""), None, [chatbot, chat_id_state])
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  demo.launch(theme=gr.themes.Soft(), css="styles.css")
 
6
  """
7
 
8
 
 
 
 
 
9
  import gradio as gr
10
  from core_logic import chat_function
11
  from storage import save_chat, load_history, get_chat_content
12
 
13
  with gr.Blocks() as demo:
14
+ # This state keeps track of the filename for the current session
15
  chat_id_state = gr.State("")
16
 
17
  with gr.Row():
18
+ # --- Sidebar ---
19
  with gr.Column(scale=1, variant="secondary"):
20
  gr.Markdown("### 🛠️ Silicon Architect")
21
  new_btn = gr.Button("➕ New Chat", variant="primary")
22
+
23
+ # The sidebar component
24
  history_list = gr.Dataset(
25
  components=[gr.Textbox(visible=False)],
26
  label="Recent Conversations",
27
+ samples=load_history(),
28
+ type="values" # Ensures we get the string value when clicked
29
  )
30
 
31
+ # --- Main Chat ---
32
  with gr.Column(scale=4):
33
  chatbot = gr.Chatbot(show_label=False, height=700)
34
  chat_input = gr.MultimodalTextbox(
 
37
  show_label=False
38
  )
39
 
40
+ # --- LOGIC FUNCTIONS ---
41
+
42
+ def bot_response(message, history, chat_id):
43
  user_content = message["text"]
44
  history.append({"role": "user", "content": user_content})
 
 
45
  history.append({"role": "assistant", "content": ""})
46
 
 
 
47
  for partial_resp in chat_function(message, history[:-1]):
48
  history[-1]["content"] = partial_resp
49
  yield history
50
 
51
+ def handle_save(history, chat_id):
52
+ # Saves the chat and returns the updated list for the sidebar
53
+ new_id = save_chat(chat_id, history)
54
+ return new_id, load_history()
55
+
56
+ def load_past_chat(selected_list):
57
+ # selected_list comes as [ 'chat_id' ]
58
+ chat_id = selected_list[0]
59
+ content = get_chat_content(chat_id)
60
+ return content, chat_id
61
+
62
+ # --- EVENT HANDLERS ---
63
+
64
+ # 1. Submit Chat -> Stream Response -> Save -> Refresh Sidebar
65
+ chat_input.submit(
66
+ bot_response,
67
+ [chat_input, chatbot, chat_id_state],
68
+ [chatbot]
69
+ ).then(
70
+ handle_save,
71
+ [chatbot, chat_id_state],
72
+ [chat_id_state, history_list]
73
  )
74
 
75
+ # 2. Click Sidebar Item -> Load Content
76
+ history_list.click(
77
+ load_past_chat,
78
+ [history_list],
79
+ [chatbot, chat_id_state]
80
+ )
81
+
82
+ # 3. New Chat Button
83
+ new_btn.click(
84
+ lambda: ([], "", load_history()),
85
+ None,
86
+ [chatbot, chat_id_state, history_list]
87
+ )
88
 
89
  demo.launch(theme=gr.themes.Soft(), css="styles.css")
app_earlier.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 
2
+ # ./app.py
3
+
4
+ """
5
+ The Interface Skeleton - The code sets up the navigation panel and the multimodal chat interface
6
+ """
7
+
8
+
9
+ import gradio as gr
10
+ from core_logic import chat_function
11
+ from storage import save_chat, load_history, get_chat_content
12
+
13
+ with gr.Blocks() as demo:
14
+ chat_id_state = gr.State("")
15
+
16
+ with gr.Row():
17
+ with gr.Column(scale=1, variant="secondary"):
18
+ gr.Markdown("### 🛠️ Silicon Architect")
19
+ new_btn = gr.Button("➕ New Chat", variant="primary")
20
+ history_list = gr.Dataset(
21
+ components=[gr.Textbox(visible=False)],
22
+ label="Recent Conversations",
23
+ samples=load_history()
24
+ )
25
+
26
+ with gr.Column(scale=4):
27
+ chatbot = gr.Chatbot(show_label=False, height=700)
28
+ chat_input = gr.MultimodalTextbox(
29
+ interactive=True,
30
+ placeholder="Discuss architecture or upload code...",
31
+ show_label=False
32
+ )
33
+
34
+ def bot_response(message, history):
35
+ # 1. Add User Message
36
+ user_content = message["text"]
37
+ history.append({"role": "user", "content": user_content})
38
+
39
+ # 2. Add empty Assistant Message to be filled
40
+ history.append({"role": "assistant", "content": ""})
41
+
42
+ # 3. Stream the response
43
+ # history[:-1] sends everything EXCEPT the empty assistant slot we just made
44
+ for partial_resp in chat_function(message, history[:-1]):
45
+ history[-1]["content"] = partial_resp
46
+ yield history
47
+
48
+ # Event Handlers
49
+ chat_input.submit(bot_response, [chat_input, chatbot], [chatbot]).then(
50
+ lambda h: save_chat(None, h), [chatbot], None
51
+ )
52
+
53
+ new_btn.click(lambda: ([], ""), None, [chatbot, chat_id_state])
54
+
55
+ demo.launch(theme=gr.themes.Soft(), css="styles.css")
storage.py CHANGED
@@ -51,14 +51,10 @@ def load_history():
51
  try:
52
  # We pull the list from the Hub so the sidebar reflects all saved sessions
53
  files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset")
54
- chat_files = [
55
- f.split("/")[-1].replace(".json", "")
56
- for f in files if f.startswith("chats/")
57
- ]
58
- # Return as list of lists for Gradio Dataset component
59
  return [[f] for f in sorted(chat_files, reverse=True)]
60
- except Exception as e:
61
- print(f"Error loading history from Hub: {e}")
62
  return []
63
 
64
  def get_chat_content(chat_id):
 
51
  try:
52
  # We pull the list from the Hub so the sidebar reflects all saved sessions
53
  files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset")
54
+ chat_files = [f.split("/")[-1].replace(".json", "") for f in files if f.startswith("chats/")]
55
+ # IMPORTANT: Sorted by newest first; return as list of lists for Gradio Dataset component
 
 
 
56
  return [[f] for f in sorted(chat_files, reverse=True)]
57
+ except:
 
58
  return []
59
 
60
  def get_chat_content(chat_id):
styles.css CHANGED
@@ -21,4 +21,14 @@
21
  button.primary {
22
  background: #007bff !important;
23
  border: none !important;
 
 
 
 
 
 
 
 
 
 
24
  }
 
21
  button.primary {
22
  background: #007bff !important;
23
  border: none !important;
24
+ }
25
+
26
+ /* Making sidebar items look professional and clickable */
27
+ .gr-dataset {
28
+ cursor: pointer;
29
+ border: none !important;
30
+ }
31
+
32
+ .gr-dataset:hover {
33
+ background-color: #e8eaed !important;
34
  }