Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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(
|
| 127 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 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 |
-
#
|
| 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.
|
| 148 |
edit_btn.click(edit_message, inputs=[state, edit_index, edit_text], outputs=[chatbot])
|
| 149 |
|
| 150 |
demo.css = """
|
| 151 |
-
.gradio-container {
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
"""
|
| 155 |
|
| 156 |
# ----------------------------
|
| 157 |
-
# Run Gradio + FastAPI
|
| 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)
|