Sachin5112 commited on
Commit
478380c
Β·
verified Β·
1 Parent(s): 83ac8dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -80,13 +80,13 @@ def copy_last(history):
80
  return ""
81
 
82
  def download_history(history):
83
- file_path = "chat_history.txt"
84
  text = ""
85
  for h in history:
86
  if isinstance(h, dict):
87
  text += f'{h["role"]}: {h["message"]}\n\n'
88
  else:
89
  text += f'User: {h[0]}\nAI: {h[1]}\n\n'
 
90
  with open(file_path, "w", encoding="utf-8") as f:
91
  f.write(text)
92
  return file_path
@@ -123,38 +123,50 @@ def chat_endpoint(request: ChatRequest):
123
  # ----------------------------
124
  # Gradio UI
125
  # ----------------------------
126
- with gr.Blocks(theme=gr.Theme.from_hub("JackismyShephard/ultimate-rvc-theme")) as demo:
127
- gr.HTML("<h2 style='text-align:center; color:white;'>Advanced AI Chat</h2>")
 
 
 
 
128
 
129
- chatbot = gr.Chatbot(height=600)
130
- msg = gr.Textbox(placeholder="Type your question...", container=False)
131
- state = gr.State([])
 
 
132
 
133
- send_btn = gr.Button("Send")
134
- stop_btn = gr.Button("πŸ›‘ Stop")
135
- copy_btn = gr.Button("πŸ“‹ Copy Last")
136
- download_btn = gr.File(label="πŸ“₯ Download Chat")
137
- edit_index = gr.Number(label="Edit Message Index")
138
- edit_text = gr.Textbox(label="New Text")
139
- edit_btn = gr.Button("✏️ Edit")
140
 
141
  # ----------------------------
142
- # Button callbacks
143
  # ----------------------------
144
  send_btn.click(generate_response, [msg, state], [chatbot, state])
145
  stop_btn.click(lambda: stop_event.set())
146
  copy_btn.click(copy_last, inputs=[state], outputs=None)
147
- download_btn.click(download_history, inputs=[state], outputs=download_btn)
148
  edit_btn.click(edit_message, inputs=[state, edit_index, edit_text], outputs=[chatbot])
149
 
150
  demo.css = """
151
- .gradio-container { border-radius: 25px !important; max-width: 600px !important; margin:auto !important; overflow:hidden; }
152
- .message.user { border-radius: 18px 18px 4px 18px !important; }
153
- .message.bot { border-radius: 18px 18px 18px 4px !important; }
 
 
 
 
 
 
 
 
 
154
  """
155
 
156
  # ----------------------------
157
- # Run Gradio + FastAPI together
158
  # ----------------------------
159
  def run_gradio():
160
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
80
  return ""
81
 
82
  def download_history(history):
 
83
  text = ""
84
  for h in history:
85
  if isinstance(h, dict):
86
  text += f'{h["role"]}: {h["message"]}\n\n'
87
  else:
88
  text += f'User: {h[0]}\nAI: {h[1]}\n\n'
89
+ file_path = "chat_history.txt"
90
  with open(file_path, "w", encoding="utf-8") as f:
91
  f.write(text)
92
  return file_path
 
123
  # ----------------------------
124
  # Gradio UI
125
  # ----------------------------
126
+ with gr.Blocks() as demo:
127
+ with gr.Row():
128
+ with gr.Column(scale=1, min_width=800):
129
+ chatbot = gr.Chatbot(height=700)
130
+ msg = gr.Textbox(placeholder="Type your message...", container=False)
131
+ state = gr.State([])
132
 
133
+ with gr.Row():
134
+ send_btn = gr.Button("Send")
135
+ stop_btn = gr.Button("πŸ›‘ Stop")
136
+ copy_btn = gr.Button("πŸ“‹ Copy Last")
137
+ download_btn = gr.File(label="πŸ“₯ Download Chat")
138
 
139
+ with gr.Row():
140
+ edit_index = gr.Number(label="Edit Index")
141
+ edit_text = gr.Textbox(label="New Text")
142
+ edit_btn = gr.Button("✏️ Edit")
 
 
 
143
 
144
  # ----------------------------
145
+ # Callbacks
146
  # ----------------------------
147
  send_btn.click(generate_response, [msg, state], [chatbot, state])
148
  stop_btn.click(lambda: stop_event.set())
149
  copy_btn.click(copy_last, inputs=[state], outputs=None)
150
+ download_btn.update(value=None) # gr.File doesn't have click; we update value
151
  edit_btn.click(edit_message, inputs=[state, edit_index, edit_text], outputs=[chatbot])
152
 
153
  demo.css = """
154
+ .gradio-container {
155
+ width: 100vw !important;
156
+ height: 100vh !important;
157
+ border-radius: 0px !important;
158
+ overflow: hidden;
159
+ background-color: #0b0f19 !important;
160
+ }
161
+ .input-textbox textarea {
162
+ border-radius: 25px !important;
163
+ }
164
+ .message.user { border-radius: 18px 18px 4px 18px !important; background:#2b6fff !important; color:white !important;}
165
+ .message.bot { border-radius: 18px 18px 18px 4px !important; background:#1c1f2a !important; color:white !important;}
166
  """
167
 
168
  # ----------------------------
169
+ # Run Gradio + FastAPI
170
  # ----------------------------
171
  def run_gradio():
172
  demo.launch(server_name="0.0.0.0", server_port=7860)