neuralworm commited on
Commit
4718250
·
1 Parent(s): b7f3dcc

UI: Simplified layout and fixed overflow bug

Browse files
Files changed (1) hide show
  1. ui_module.py +40 -36
ui_module.py CHANGED
@@ -2,6 +2,9 @@ import gradio as gr
2
  import uuid
3
  import logging
4
  import re
 
 
 
5
  from agent_module import chat_agent_stream
6
  from translation_module import localize_init
7
 
@@ -9,22 +12,38 @@ logger = logging.getLogger("app.ui")
9
 
10
  PREMIUM_CSS = """
11
  body, .gradio-container { background-color: #0b0f19 !important; color: #e0e6ed !important; font-family: 'Inter', sans-serif !important; }
12
- .main-container { max-width: 900px !important; margin: 0 auto !important; height: 100vh !important; display: flex !important; flex-direction: column !important; padding: 10px !important; }
13
  .header-row { display: flex !important; align-items: center !important; justify-content: space-between !important; gap: 10px !important; margin-bottom: 10px !important; padding: 10px !important; background: rgba(255, 255, 255, 0.05) !important; border-radius: 12px !important; }
14
- .chat-window { flex-grow: 1 !important; overflow-y: auto !important; border-radius: 12px !important; border: 1px solid rgba(255, 255, 255, 0.1) !important; background: rgba(0, 0, 0, 0.2) !important; }
15
- .input-area { margin-top: 10px !important; background: rgba(255, 255, 255, 0.05) !important; padding: 10px !important; border-radius: 12px !important; }
16
  footer { display: none !important; }
17
- @media (max-width: 768px) { .main-container { padding: 5px !important; height: 100vh !important; } .header-row { padding: 5px !important; flex-wrap: wrap !important; } }
18
  """
19
 
20
  def save_and_clear(msg):
21
  return "", msg
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def build_demo(enable_reload=True):
24
  with gr.Blocks(css=PREMIUM_CSS, title="Sage 6.5 - Oracle Intermediary") as demo:
25
  # State
26
- threads = gr.State({})
27
- tid = gr.State(str(uuid.uuid4()))
28
  lang_state = gr.State("English")
29
  saved_msg = gr.State("")
30
 
@@ -33,8 +52,8 @@ def build_demo(enable_reload=True):
33
  with gr.Row(elem_classes="header-row"):
34
  gr.Markdown("## 🔮 Sage 6.5")
35
  with gr.Row():
36
- thread_dropdown = gr.Dropdown(label="Conversations", choices=[], interactive=True)
37
- new_btn = gr.Button("", variant="secondary")
38
 
39
  # Chat
40
  chatbot = gr.Chatbot(elem_classes="chat-window", type="messages", bubble_full_width=False, show_label=False)
@@ -47,56 +66,41 @@ def build_demo(enable_reload=True):
47
  short_answers = gr.Checkbox(label="Short Answers", value=False)
48
 
49
  # -- Logic --
50
- def chat_wrapper(message, history, short_answers=False, threads=None, tid=None, lang="English"):
51
- if threads is None: threads = {}
52
- if tid is None: tid = str(uuid.uuid4())
53
 
54
- # Instant Name Detection: If user says something that looks like a name
55
- # and history is empty (or just welcome msg).
56
  history.append({"role": "user", "content": message})
57
  history.append({"role": "assistant", "content": ""})
58
 
59
- # Check for name-only or "I am X" etc.
60
- # If detected, we can force a tool call result into the first turn.
61
  name_match = re.search(r"(?:mein name ist|ich bin|i am|my name is)\s+([a-zA-z\s]+)", message, re.I)
 
62
  if not name_match and len(message.split()) < 3 and message[0].isupper():
63
  name_val = message.strip()
64
  elif name_match:
65
  name_val = name_match.group(1).strip()
66
- else:
67
- name_val = None
68
-
69
- force_topic = None
70
- if name_val and len(history) <= 3: # First real turn
71
- force_topic = "personal destiny"
72
- logger.info(f"Instant name trigger detected: {name_val}")
73
 
74
  for part in chat_agent_stream(message, history[:-2], user_lang=lang, short_answers=short_answers, force_tool_call=(name_val is not None), forced_name=name_val):
75
  if part == "__TURN_END__":
76
  history.append({"role": "assistant", "content": ""})
77
- yield history, threads, gr.update(), gr.update()
78
  else:
79
  history[-1]["content"] = part
80
- yield history, threads, gr.update(), gr.update()
81
 
82
- if tid not in threads: threads[tid] = {"history": history, "title": message[:20]}
83
- threads[tid]["history"] = history
84
- choices = [(v["title"], k) for k, v in threads.items()]
85
-
86
- # API compatibility check
87
- if threads is None: # Called via simple API
88
- yield history, gr.update(choices=choices, value=tid)
89
- else:
90
- yield history, threads, gr.update(choices=choices, value=tid), gr.update()
91
 
92
  # Wire Events
93
  msg_input.submit(save_and_clear, [msg_input], [msg_input, saved_msg], queue=False).then(
94
- chat_wrapper, [saved_msg, chatbot, short_answers, threads, tid, lang_state], [chatbot, threads, thread_dropdown]
95
  )
96
  submit_btn.click(save_and_clear, [msg_input], [msg_input, saved_msg], queue=False).then(
97
- chat_wrapper, [saved_msg, chatbot, short_answers, threads, tid, lang_state], [chatbot, threads, thread_dropdown]
98
  )
99
 
 
 
 
100
  def on_load(request: gr.Request):
101
  if not enable_reload:
102
  return [], "English", gr.update(), gr.update()
@@ -113,6 +117,6 @@ def build_demo(enable_reload=True):
113
 
114
  # API
115
  api_chat_btn = gr.Button("API CHAT", visible=False)
116
- api_chat_btn.click(chat_wrapper, [msg_input, chatbot, short_answers], [chatbot, thread_dropdown], api_name="/chat")
117
 
118
  return demo
 
2
  import uuid
3
  import logging
4
  import re
5
+ import json
6
+ import os
7
+ import tempfile
8
  from agent_module import chat_agent_stream
9
  from translation_module import localize_init
10
 
 
12
 
13
  PREMIUM_CSS = """
14
  body, .gradio-container { background-color: #0b0f19 !important; color: #e0e6ed !important; font-family: 'Inter', sans-serif !important; }
15
+ .main-container { max-width: 900px !important; margin: 0 auto !important; min-height: 600px !important; max-height: 90vh !important; display: flex !important; flex-direction: column !important; padding: 10px !important; background: #0b0f19 !important; }
16
  .header-row { display: flex !important; align-items: center !important; justify-content: space-between !important; gap: 10px !important; margin-bottom: 10px !important; padding: 10px !important; background: rgba(255, 255, 255, 0.05) !important; border-radius: 12px !important; }
17
+ .chat-window { flex-grow: 1 !important; border-radius: 12px !important; border: 1px solid rgba(255, 255, 255, 0.1) !important; background: rgba(0, 0, 0, 0.2) !important; overflow-y: auto !important; }
18
+ .input-area { margin-top: 10px !important; background: rgba(255, 255, 255, 0.05) !important; padding: 10px !important; border-radius: 12px !important; flex-shrink: 0 !important; }
19
  footer { display: none !important; }
20
+ @media (max-width: 768px) { .main-container { padding: 5px !important; height: auto !important; max-height: none !important; } .header-row { padding: 5px !important; flex-wrap: wrap !important; } }
21
  """
22
 
23
  def save_and_clear(msg):
24
  return "", msg
25
 
26
+ def export_chat(history):
27
+ if not history: return None
28
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as tf:
29
+ json.dump(history, tf, indent=2)
30
+ return tf.name
31
+
32
+ def import_chat(file):
33
+ if file is None: return gr.update(), []
34
+ try:
35
+ with open(file.name, 'r') as f:
36
+ data = json.load(f)
37
+ if isinstance(data, list):
38
+ return data, data
39
+ except Exception as e:
40
+ logger.error(f"Import error: {e}")
41
+ return gr.update(), gr.update()
42
+
43
  def build_demo(enable_reload=True):
44
  with gr.Blocks(css=PREMIUM_CSS, title="Sage 6.5 - Oracle Intermediary") as demo:
45
  # State
46
+ history_state = gr.State([])
 
47
  lang_state = gr.State("English")
48
  saved_msg = gr.State("")
49
 
 
52
  with gr.Row(elem_classes="header-row"):
53
  gr.Markdown("## 🔮 Sage 6.5")
54
  with gr.Row():
55
+ import_btn = gr.UploadButton("📥 Import", file_types=[".json"], size="sm")
56
+ export_btn = gr.DownloadButton("📤 Export", size="sm")
57
 
58
  # Chat
59
  chatbot = gr.Chatbot(elem_classes="chat-window", type="messages", bubble_full_width=False, show_label=False)
 
66
  short_answers = gr.Checkbox(label="Short Answers", value=False)
67
 
68
  # -- Logic --
69
+ def chat_wrapper(message, history, short_answers=False, lang="English"):
70
+ if history is None: history = []
 
71
 
 
 
72
  history.append({"role": "user", "content": message})
73
  history.append({"role": "assistant", "content": ""})
74
 
75
+ # Name Detection
 
76
  name_match = re.search(r"(?:mein name ist|ich bin|i am|my name is)\s+([a-zA-z\s]+)", message, re.I)
77
+ name_val = None
78
  if not name_match and len(message.split()) < 3 and message[0].isupper():
79
  name_val = message.strip()
80
  elif name_match:
81
  name_val = name_match.group(1).strip()
 
 
 
 
 
 
 
82
 
83
  for part in chat_agent_stream(message, history[:-2], user_lang=lang, short_answers=short_answers, force_tool_call=(name_val is not None), forced_name=name_val):
84
  if part == "__TURN_END__":
85
  history.append({"role": "assistant", "content": ""})
86
+ yield history, history
87
  else:
88
  history[-1]["content"] = part
89
+ yield history, history
90
 
91
+ yield history, history
 
 
 
 
 
 
 
 
92
 
93
  # Wire Events
94
  msg_input.submit(save_and_clear, [msg_input], [msg_input, saved_msg], queue=False).then(
95
+ chat_wrapper, [saved_msg, history_state, short_answers, lang_state], [chatbot, history_state]
96
  )
97
  submit_btn.click(save_and_clear, [msg_input], [msg_input, saved_msg], queue=False).then(
98
+ chat_wrapper, [saved_msg, history_state, short_answers, lang_state], [chatbot, history_state]
99
  )
100
 
101
+ export_btn.click(export_chat, [history_state], [export_btn])
102
+ import_btn.upload(import_chat, [import_btn], [chatbot, history_state])
103
+
104
  def on_load(request: gr.Request):
105
  if not enable_reload:
106
  return [], "English", gr.update(), gr.update()
 
117
 
118
  # API
119
  api_chat_btn = gr.Button("API CHAT", visible=False)
120
+ api_chat_btn.click(chat_wrapper, [msg_input, chatbot, short_answers], [chatbot, history_state], api_name="/chat")
121
 
122
  return demo