solve const issue
Browse files- app.py +46 -5
- patch_gradio.py +5 -4
app.py
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from pathlib import Path
|
| 3 |
from tempfile import mkdtemp
|
| 4 |
|
| 5 |
-
import patch_gradio
|
| 6 |
-
|
| 7 |
# LangChain & Embedding/LLM
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
|
|
@@ -198,6 +220,25 @@ def chat_with_doc(query):
|
|
| 198 |
|
| 199 |
return answer, source_text
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
# Gradio Interface
|
| 202 |
with gr.Blocks() as app:
|
| 203 |
gr.Markdown("# Document Q&A System")
|
|
@@ -214,7 +255,7 @@ with gr.Blocks() as app:
|
|
| 214 |
file_output = gr.Textbox(label="Processing Status", interactive=False)
|
| 215 |
|
| 216 |
process_btn.click(
|
| 217 |
-
fn=
|
| 218 |
inputs=[file_input, extraction_method],
|
| 219 |
outputs=file_output
|
| 220 |
)
|
|
@@ -226,13 +267,13 @@ with gr.Blocks() as app:
|
|
| 226 |
sources_output = gr.Textbox(label="Sources", interactive=False)
|
| 227 |
|
| 228 |
ask_btn.click(
|
| 229 |
-
fn=
|
| 230 |
inputs=chat_input,
|
| 231 |
outputs=[chat_output, sources_output]
|
| 232 |
)
|
| 233 |
|
| 234 |
chat_input.submit(
|
| 235 |
-
fn=
|
| 236 |
inputs=chat_input,
|
| 237 |
outputs=[chat_output, sources_output]
|
| 238 |
)
|
|
|
|
| 1 |
+
# First import our patch before any gradio imports
|
| 2 |
+
try:
|
| 3 |
+
from patch_gradio import patched_get_type
|
| 4 |
+
except ImportError:
|
| 5 |
+
# Create patch on the fly if file doesn't exist
|
| 6 |
+
from types import MethodType
|
| 7 |
+
import gradio_client.utils
|
| 8 |
+
|
| 9 |
+
# Store the original function
|
| 10 |
+
original_get_type = gradio_client.utils.get_type
|
| 11 |
+
|
| 12 |
+
# Define the patched function
|
| 13 |
+
def patched_get_type(schema):
|
| 14 |
+
# Check if schema is a dict before using 'in' operator
|
| 15 |
+
if not isinstance(schema, dict):
|
| 16 |
+
return str(schema) # Convert to string as fallback
|
| 17 |
+
|
| 18 |
+
# Original logic continues
|
| 19 |
+
return original_get_type(schema)
|
| 20 |
+
|
| 21 |
+
# Apply the patch
|
| 22 |
+
gradio_client.utils.get_type = patched_get_type
|
| 23 |
+
|
| 24 |
+
# Now import the rest of the packages
|
| 25 |
import gradio as gr
|
| 26 |
from pathlib import Path
|
| 27 |
from tempfile import mkdtemp
|
| 28 |
|
|
|
|
|
|
|
| 29 |
# LangChain & Embedding/LLM
|
| 30 |
from langchain_community.vectorstores import FAISS
|
| 31 |
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 220 |
|
| 221 |
return answer, source_text
|
| 222 |
|
| 223 |
+
# Graceful error handling wrapper for Gradio functions
|
| 224 |
+
def safe_process_file(file, extraction_method):
|
| 225 |
+
try:
|
| 226 |
+
return process_file(file, extraction_method)
|
| 227 |
+
except Exception as e:
|
| 228 |
+
import traceback
|
| 229 |
+
error_trace = traceback.format_exc()
|
| 230 |
+
print(f"Error processing file: {str(e)}\n{error_trace}")
|
| 231 |
+
return f"Error processing file: {str(e)}"
|
| 232 |
+
|
| 233 |
+
def safe_chat_with_doc(query):
|
| 234 |
+
try:
|
| 235 |
+
return chat_with_doc(query)
|
| 236 |
+
except Exception as e:
|
| 237 |
+
import traceback
|
| 238 |
+
error_trace = traceback.format_exc()
|
| 239 |
+
print(f"Error processing query: {str(e)}\n{error_trace}")
|
| 240 |
+
return f"Error processing query: {str(e)}", ""
|
| 241 |
+
|
| 242 |
# Gradio Interface
|
| 243 |
with gr.Blocks() as app:
|
| 244 |
gr.Markdown("# Document Q&A System")
|
|
|
|
| 255 |
file_output = gr.Textbox(label="Processing Status", interactive=False)
|
| 256 |
|
| 257 |
process_btn.click(
|
| 258 |
+
fn=safe_process_file, # Use the wrapped safe function
|
| 259 |
inputs=[file_input, extraction_method],
|
| 260 |
outputs=file_output
|
| 261 |
)
|
|
|
|
| 267 |
sources_output = gr.Textbox(label="Sources", interactive=False)
|
| 268 |
|
| 269 |
ask_btn.click(
|
| 270 |
+
fn=safe_chat_with_doc, # Use the wrapped safe function
|
| 271 |
inputs=chat_input,
|
| 272 |
outputs=[chat_output, sources_output]
|
| 273 |
)
|
| 274 |
|
| 275 |
chat_input.submit(
|
| 276 |
+
fn=safe_chat_with_doc, # Use the wrapped safe function
|
| 277 |
inputs=chat_input,
|
| 278 |
outputs=[chat_output, sources_output]
|
| 279 |
)
|
patch_gradio.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
| 1 |
# Create a file called patch_gradio.py
|
|
|
|
| 2 |
import gradio_client.utils
|
| 3 |
|
| 4 |
# Store the original function
|
| 5 |
original_get_type = gradio_client.utils.get_type
|
| 6 |
|
| 7 |
# Define the patched function
|
| 8 |
-
def patched_get_type(schema):
|
| 9 |
-
#
|
| 10 |
if not isinstance(schema, dict):
|
| 11 |
return str(schema) # Convert to string as fallback
|
| 12 |
|
| 13 |
-
# Original
|
| 14 |
-
return original_get_type(schema)
|
| 15 |
|
| 16 |
# Apply the patch
|
| 17 |
gradio_client.utils.get_type = patched_get_type
|
|
|
|
| 1 |
# Create a file called patch_gradio.py
|
| 2 |
+
from types import MethodType
|
| 3 |
import gradio_client.utils
|
| 4 |
|
| 5 |
# Store the original function
|
| 6 |
original_get_type = gradio_client.utils.get_type
|
| 7 |
|
| 8 |
# Define the patched function
|
| 9 |
+
def patched_get_type(self, schema):
|
| 10 |
+
# Check if schema is a dict before using 'in' operator
|
| 11 |
if not isinstance(schema, dict):
|
| 12 |
return str(schema) # Convert to string as fallback
|
| 13 |
|
| 14 |
+
# Original logic continues
|
| 15 |
+
return original_get_type(self, schema)
|
| 16 |
|
| 17 |
# Apply the patch
|
| 18 |
gradio_client.utils.get_type = patched_get_type
|