Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,138 +1,40 @@
|
|
| 1 |
-
# app.py — MINIMAL TEST VERSION (Revised State Handling)
|
| 2 |
import gradio as gr
|
| 3 |
-
import time
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
print(f"[{time.time()}] SCRIPT START:
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
print(f"[{time.time()}] dummy_add_pdfs called. Files: {files}. Current history length: {len(current_chat_history if current_chat_history else [])}")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
for i, f_obj in enumerate(files):
|
| 17 |
-
# Use original filename if available (Gradio often provides this)
|
| 18 |
-
# Fallback to a generic name if orig_name is not present
|
| 19 |
-
original_name = getattr(f_obj, 'orig_name', f"Uploaded File {i+1}")
|
| 20 |
-
mock_pdf_names.append(f"{original_name} (mock)")
|
| 21 |
-
print(f"[{time.time()}] Mock processing file: {original_name}")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
print(f"[{time.time()}] dummy_add_pdfs: Dropdown choices: {mock_pdf_names}, Selected: {selected_choice}")
|
| 26 |
-
|
| 27 |
-
# Return the updated choices for the dropdown and the original chat history (as it's not modified here)
|
| 28 |
-
return gr.Dropdown.update(choices=mock_pdf_names, value=selected_choice), current_chat_history
|
| 29 |
-
|
| 30 |
-
def dummy_chat(query, selected_pdf_from_dropdown, current_chat_history):
|
| 31 |
-
# query is the user's text
|
| 32 |
-
# selected_pdf_from_dropdown is the value from the pdf_select Dropdown
|
| 33 |
-
# current_chat_history is the current list of chat messages (our app_state)
|
| 34 |
-
print(f"[{time.time()}] dummy_chat called. Query: '{query}', Selected PDF: '{selected_pdf_from_dropdown}'. Current history length: {len(current_chat_history if current_chat_history else [])}")
|
| 35 |
-
|
| 36 |
-
# Ensure current_chat_history is a list
|
| 37 |
-
if current_chat_history is None:
|
| 38 |
-
current_chat_history = []
|
| 39 |
-
|
| 40 |
-
if not selected_pdf_from_dropdown:
|
| 41 |
-
answer = "Please upload and select a PDF first (mock)."
|
| 42 |
-
else:
|
| 43 |
-
answer = f"This is a **mock** response to '{query}' for PDF: '{selected_pdf_from_dropdown}'. Model loading is disabled."
|
| 44 |
-
|
| 45 |
-
new_chat_history = current_chat_history + [[query, answer]]
|
| 46 |
-
|
| 47 |
-
print(f"[{time.time()}] dummy_chat: New history length: {len(new_chat_history)}")
|
| 48 |
-
# Return the updated chat history for the chatbot and for the state
|
| 49 |
-
return new_chat_history, new_chat_history
|
| 50 |
-
|
| 51 |
-
def clear_chat_and_query():
|
| 52 |
-
print(f"[{time.time()}] clear_chat_and_query called.")
|
| 53 |
-
# Returns: new value for chatbot, new value for app_state (chat history), new value for query_box
|
| 54 |
-
return [], [], ""
|
| 55 |
-
|
| 56 |
-
print(f"[{time.time()}] Building Gradio interface (minimal v2)...")
|
| 57 |
-
with gr.Blocks(theme=gr.themes.Soft(), css="footer {display:none}") as demo:
|
| 58 |
-
gr.Markdown("## 📄 Minimal Test: PDF Chat App (Models Disabled - v2)")
|
| 59 |
-
gr.Markdown("### If you see this, Gradio started. Model loading is bypassed. State simplified.")
|
| 60 |
-
|
| 61 |
-
# app_state will now only be the chat_history list
|
| 62 |
-
app_state_chat_history = gr.State([])
|
| 63 |
-
|
| 64 |
-
with gr.Row():
|
| 65 |
-
with gr.Column(scale=1, min_width=300):
|
| 66 |
-
file_box = gr.File(
|
| 67 |
-
label="Upload PDF(s) (Mock)",
|
| 68 |
-
file_types=[".pdf"],
|
| 69 |
-
file_count="multiple"
|
| 70 |
-
)
|
| 71 |
-
pdf_select = gr.Dropdown(
|
| 72 |
-
label="Choose a PDF (Mock)",
|
| 73 |
-
interactive=True
|
| 74 |
-
)
|
| 75 |
-
with gr.Column(scale=3, min_width=500):
|
| 76 |
-
chatbot = gr.Chatbot(
|
| 77 |
-
label="Conversation (Mock)",
|
| 78 |
-
bubble_full_width=False,
|
| 79 |
-
height=500,
|
| 80 |
-
show_copy_button=True
|
| 81 |
-
)
|
| 82 |
-
query_box = gr.Textbox(
|
| 83 |
-
label="Ask a question (Mock)…",
|
| 84 |
-
placeholder="Type your question here and press Enter.",
|
| 85 |
-
)
|
| 86 |
-
clear_button = gr.Button("Clear Chat")
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
# Re-thinking file_box.upload outputs:
|
| 99 |
-
# dummy_add_pdfs should primarily update the pdf_select dropdown.
|
| 100 |
-
# It doesn't need to modify or output the chat history state.
|
| 101 |
-
# So, app_state_chat_history is NOT an output of file_box.upload.
|
| 102 |
-
|
| 103 |
-
# Corrected file_box.upload call:
|
| 104 |
-
file_box.upload(
|
| 105 |
-
fn=lambda files: gr.Dropdown.update(choices=[getattr(f, 'orig_name', f"File {i+1}") + " (mock)" for i, f in enumerate(files)] if files else [],
|
| 106 |
-
value=(getattr(files[0], 'orig_name', "File 1") + " (mock)" if files else None)),
|
| 107 |
-
inputs=[file_box],
|
| 108 |
-
outputs=[pdf_select]
|
| 109 |
-
)
|
| 110 |
-
# This lambda simplifies dummy_add_pdfs significantly.
|
| 111 |
-
# Let's remove dummy_add_pdfs and use this lambda directly.
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
query_box.submit(
|
| 115 |
-
fn=dummy_chat,
|
| 116 |
-
inputs=[query_box, pdf_select, app_state_chat_history], # pdf_select provides the chosen PDF name
|
| 117 |
-
outputs=[chatbot, app_state_chat_history] # chatbot gets new history, state gets new history
|
| 118 |
)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
fn=clear_chat_and_query,
|
| 122 |
-
inputs=None, # Clear doesn't need state input if it just resets to empty
|
| 123 |
-
outputs=[chatbot, app_state_chat_history, query_box]
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
print(f"[{time.time()}] Gradio Blocks defined (minimal v2).")
|
| 127 |
-
|
| 128 |
-
if __name__ == "__main__":
|
| 129 |
-
print(f"[{time.time()}] MAIN: Attempting to launch Gradio app (minimal v2)...")
|
| 130 |
try:
|
| 131 |
-
|
| 132 |
-
print(f"[{time.time()}] MAIN: Gradio app
|
| 133 |
except Exception as e:
|
| 134 |
-
print(f"[{time.time()}] FATAL ERROR during
|
|
|
|
| 135 |
with open("launch_error.txt", "w") as f_err:
|
| 136 |
-
f_err.write(str(e))
|
| 137 |
|
| 138 |
-
print(f"[{time.time()}] SCRIPT END:
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
print(f"[{time.time()}] SCRIPT START: Echo Bot Test. PID: {os.getpid()}")
|
| 6 |
|
| 7 |
+
def echo_chat(message, history):
|
| 8 |
+
# message: The user's input string
|
| 9 |
+
# history: A list of previous interactions [[user_msg_1, bot_msg_1], [user_msg_2, bot_msg_2], ...]
|
| 10 |
+
print(f"[{time.time()}] echo_chat called. Message: '{message}'")
|
|
|
|
| 11 |
|
| 12 |
+
# Simulate a little bit of work
|
| 13 |
+
time.sleep(0.2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# The function for gr.ChatInterface should return the bot's response as a string
|
| 16 |
+
return f"Echo: {message}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
if __name__ == "__main__":
|
| 19 |
+
print(f"[{time.time()}] MAIN: Building Gradio interface (Echo Bot)...")
|
| 20 |
+
|
| 21 |
+
# Using gr.ChatInterface for a very standard and robust chat UI
|
| 22 |
+
iface = gr.ChatInterface(
|
| 23 |
+
fn=echo_chat,
|
| 24 |
+
title="Echo Bot Test",
|
| 25 |
+
description="Type a message and it will be echoed back. This tests basic Gradio functionality.",
|
| 26 |
+
examples=["Hello Gradio!", "Is this working?"],
|
| 27 |
+
cache_examples=False # Disable caching for this simple test
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
+
print(f"[{time.time()}] MAIN: Attempting to launch Gradio app (Echo Bot)...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
+
iface.launch(debug=True) # debug=True gives more verbose Gradio logs
|
| 33 |
+
print(f"[{time.time()}] MAIN: Gradio app launch() called (Echo Bot). Monitor logs for 'Application startup complete'.")
|
| 34 |
except Exception as e:
|
| 35 |
+
print(f"[{time.time()}] FATAL ERROR during launch (Echo Bot): {e}")
|
| 36 |
+
# As a last resort, try to write the error to a file if logs are inaccessible
|
| 37 |
with open("launch_error.txt", "w") as f_err:
|
| 38 |
+
f_err.write(f"Error during Echo Bot launch: {str(e)}\n")
|
| 39 |
|
| 40 |
+
print(f"[{time.time()}] SCRIPT END: Echo Bot test app.py has finished executing initial setup code.")
|