Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from vllm import LLM
|
| 2 |
+
from vllm.sampling_params import SamplingParams
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
from huggingface_hub import hf_hub_download, login
|
| 5 |
+
import os
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from pdf2image import convert_from_path
|
| 8 |
+
import easyocr
|
| 9 |
+
|
| 10 |
+
# Initialize OCR reader
|
| 11 |
+
reader = easyocr.Reader(['en'])
|
| 12 |
+
|
| 13 |
+
# ... existing SYSTEM_PROMPT and load_system_prompt definitions ...
|
| 14 |
+
|
| 15 |
+
def process_pdf_or_image(file_path):
|
| 16 |
+
# Handle PDF files
|
| 17 |
+
if file_path.lower().endswith('.pdf'):
|
| 18 |
+
images = convert_from_path(file_path)
|
| 19 |
+
extracted_text = ""
|
| 20 |
+
for image in images:
|
| 21 |
+
ocr_results = reader.readtext(image, detail=0)
|
| 22 |
+
extracted_text += " ".join(ocr_results) + "\n"
|
| 23 |
+
# Handle image files
|
| 24 |
+
else:
|
| 25 |
+
ocr_results = reader.readtext(file_path, detail=0)
|
| 26 |
+
extracted_text = " ".join(ocr_results)
|
| 27 |
+
|
| 28 |
+
return extracted_text
|
| 29 |
+
|
| 30 |
+
def generate_response(file_path):
|
| 31 |
+
# Extract text from PDF/image
|
| 32 |
+
extracted_text = process_pdf_or_image(file_path)
|
| 33 |
+
|
| 34 |
+
# Prepare messages for the LLM
|
| 35 |
+
messages = [
|
| 36 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 37 |
+
{
|
| 38 |
+
"role": "user",
|
| 39 |
+
"content": [
|
| 40 |
+
{
|
| 41 |
+
"type": "text",
|
| 42 |
+
"text": f"Process this extracted text, correct any errors and enhance it:\n{extracted_text}",
|
| 43 |
+
}
|
| 44 |
+
],
|
| 45 |
+
},
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# Initialize the LLM
|
| 49 |
+
llm = LLM(model="mistralai/Mistral-Small-3.1-24B", tokenizer_mode="mistral")
|
| 50 |
+
|
| 51 |
+
# Define sampling parameters
|
| 52 |
+
sampling_params = SamplingParams(max_tokens=512, temperature=0.15)
|
| 53 |
+
|
| 54 |
+
# Get the response from the LLM
|
| 55 |
+
outputs = llm.chat(messages, sampling_params=sampling_params)
|
| 56 |
+
|
| 57 |
+
return outputs[0].outputs[0].text
|
| 58 |
+
|
| 59 |
+
# Gradio interface
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
gr.Markdown("# Document Processing with Mistral")
|
| 62 |
+
file_input = gr.File(label="Upload PDF or Image")
|
| 63 |
+
output_text = gr.Textbox(label="Processed Text", lines=10)
|
| 64 |
+
submit_btn = gr.Button("Process Document")
|
| 65 |
+
|
| 66 |
+
submit_btn.click(
|
| 67 |
+
fn=generate_response,
|
| 68 |
+
inputs=file_input,
|
| 69 |
+
outputs=output_text
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
demo.launch()
|