Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,41 +19,30 @@ def process_file(filepath):
|
|
| 19 |
if not filepath:
|
| 20 |
return ""
|
| 21 |
|
| 22 |
-
# Get file extension
|
| 23 |
-
# e.g., if filepath = "/somefolder/report.pdf", ext = ".pdf"
|
| 24 |
ext = os.path.splitext(filepath)[1].lower()
|
| 25 |
|
| 26 |
try:
|
| 27 |
if ext in ['.png', '.jpg', '.jpeg']:
|
| 28 |
-
# OCR processing for images
|
| 29 |
image = Image.open(filepath)
|
| 30 |
text = pytesseract.image_to_string(image)
|
| 31 |
return f"IMAGE CONTENT:\n{text}"
|
| 32 |
-
|
| 33 |
elif ext == '.pdf':
|
| 34 |
-
# PDF text extraction
|
| 35 |
pdf_reader = PyPDF2.PdfReader(filepath)
|
| 36 |
-
text = "\n".join(
|
| 37 |
return f"PDF CONTENT:\n{text}"
|
| 38 |
-
|
| 39 |
elif ext == '.docx':
|
| 40 |
-
# Word document processing
|
| 41 |
doc = docx.Document(filepath)
|
| 42 |
-
text = "\n".join(
|
| 43 |
return f"DOCUMENT CONTENT:\n{text}"
|
| 44 |
-
|
| 45 |
else:
|
| 46 |
return "Unsupported file type"
|
| 47 |
-
|
| 48 |
except Exception as e:
|
| 49 |
print(f"File processing error: {e}")
|
| 50 |
return "Error reading file"
|
| 51 |
|
| 52 |
def chat(message, history, filepath):
|
| 53 |
-
# Process uploaded file
|
| 54 |
file_content = process_file(filepath) if filepath else ""
|
| 55 |
|
| 56 |
-
# Build enhanced prompt
|
| 57 |
full_prompt = f"""
|
| 58 |
{file_content}
|
| 59 |
|
|
@@ -61,8 +50,7 @@ def chat(message, history, filepath):
|
|
| 61 |
|
| 62 |
Please respond considering both the message and any attached documents:
|
| 63 |
"""
|
| 64 |
-
|
| 65 |
-
# Configure generation parameters
|
| 66 |
generate_kwargs = dict(
|
| 67 |
temperature=0.7,
|
| 68 |
max_new_tokens=2000,
|
|
@@ -80,30 +68,25 @@ def chat(message, history, filepath):
|
|
| 80 |
|
| 81 |
partial_message = ""
|
| 82 |
for response in stream:
|
| 83 |
-
# Skip special tokens
|
| 84 |
if response.token.special:
|
| 85 |
continue
|
| 86 |
partial_message += response.token.text
|
| 87 |
yield partial_message
|
| 88 |
|
| 89 |
-
# Create Gradio interface with file upload
|
| 90 |
with gr.Blocks(theme="soft") as demo:
|
| 91 |
gr.Markdown("# DeepSeek-R1 Assistant with File Support")
|
| 92 |
gr.Markdown("Upload images, PDFs, or docs and chat about them!")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
-
# Note the change: type="filepath"
|
| 96 |
file_input = gr.File(label="Upload File (PDF/Image/Doc)", type="filepath")
|
| 97 |
|
| 98 |
-
# The ChatInterface should pass the file path to `chat`
|
| 99 |
chatbot = gr.ChatInterface(
|
| 100 |
fn=chat,
|
| 101 |
additional_inputs=[file_input],
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
["What's in this image?", "screenshot.png"]
|
| 105 |
-
]
|
| 106 |
)
|
| 107 |
|
| 108 |
demo.launch()
|
| 109 |
|
|
|
|
|
|
| 19 |
if not filepath:
|
| 20 |
return ""
|
| 21 |
|
|
|
|
|
|
|
| 22 |
ext = os.path.splitext(filepath)[1].lower()
|
| 23 |
|
| 24 |
try:
|
| 25 |
if ext in ['.png', '.jpg', '.jpeg']:
|
|
|
|
| 26 |
image = Image.open(filepath)
|
| 27 |
text = pytesseract.image_to_string(image)
|
| 28 |
return f"IMAGE CONTENT:\n{text}"
|
|
|
|
| 29 |
elif ext == '.pdf':
|
|
|
|
| 30 |
pdf_reader = PyPDF2.PdfReader(filepath)
|
| 31 |
+
text = "\n".join(page.extract_text() for page in pdf_reader.pages)
|
| 32 |
return f"PDF CONTENT:\n{text}"
|
|
|
|
| 33 |
elif ext == '.docx':
|
|
|
|
| 34 |
doc = docx.Document(filepath)
|
| 35 |
+
text = "\n".join(para.text for para in doc.paragraphs)
|
| 36 |
return f"DOCUMENT CONTENT:\n{text}"
|
|
|
|
| 37 |
else:
|
| 38 |
return "Unsupported file type"
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
print(f"File processing error: {e}")
|
| 41 |
return "Error reading file"
|
| 42 |
|
| 43 |
def chat(message, history, filepath):
|
|
|
|
| 44 |
file_content = process_file(filepath) if filepath else ""
|
| 45 |
|
|
|
|
| 46 |
full_prompt = f"""
|
| 47 |
{file_content}
|
| 48 |
|
|
|
|
| 50 |
|
| 51 |
Please respond considering both the message and any attached documents:
|
| 52 |
"""
|
| 53 |
+
|
|
|
|
| 54 |
generate_kwargs = dict(
|
| 55 |
temperature=0.7,
|
| 56 |
max_new_tokens=2000,
|
|
|
|
| 68 |
|
| 69 |
partial_message = ""
|
| 70 |
for response in stream:
|
|
|
|
| 71 |
if response.token.special:
|
| 72 |
continue
|
| 73 |
partial_message += response.token.text
|
| 74 |
yield partial_message
|
| 75 |
|
|
|
|
| 76 |
with gr.Blocks(theme="soft") as demo:
|
| 77 |
gr.Markdown("# DeepSeek-R1 Assistant with File Support")
|
| 78 |
gr.Markdown("Upload images, PDFs, or docs and chat about them!")
|
| 79 |
|
| 80 |
with gr.Row():
|
|
|
|
| 81 |
file_input = gr.File(label="Upload File (PDF/Image/Doc)", type="filepath")
|
| 82 |
|
|
|
|
| 83 |
chatbot = gr.ChatInterface(
|
| 84 |
fn=chat,
|
| 85 |
additional_inputs=[file_input],
|
| 86 |
+
type="messages",
|
| 87 |
+
examples=[]
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
demo.launch()
|
| 91 |
|
| 92 |
+
|