Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,36 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
def process_file(file):
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
with gr.Blocks(fill_height=True) as demo:
|
9 |
with gr.Sidebar():
|
@@ -12,10 +39,16 @@ with gr.Blocks(fill_height=True) as demo:
|
|
12 |
button = gr.LoginButton("Sign in")
|
13 |
|
14 |
with gr.Column():
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
19 |
file_input.change(process_file, inputs=file_input, outputs=file_output)
|
20 |
|
21 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import PyPDF2
|
3 |
+
import pytesseract
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Ensure Tesseract is installed and accessible
|
7 |
+
# On Windows, you may need to specify the Tesseract path:
|
8 |
+
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
9 |
|
10 |
def process_file(file):
|
11 |
+
if file is None:
|
12 |
+
return "No file uploaded."
|
13 |
+
|
14 |
+
content = ""
|
15 |
+
if file.name.endswith(".txt"):
|
16 |
+
# Read text files
|
17 |
+
with open(file.name, "r") as f:
|
18 |
+
content = f.read()
|
19 |
+
elif file.name.endswith(".pdf"):
|
20 |
+
# Extract text from PDFs
|
21 |
+
reader = PyPDF2.PdfReader(file.name)
|
22 |
+
for page in reader.pages:
|
23 |
+
content += page.extract_text()
|
24 |
+
elif file.name.endswith((".png", ".jpg", ".jpeg")):
|
25 |
+
# Extract text from images using OCR
|
26 |
+
image = Image.open(file.name)
|
27 |
+
content = pytesseract.image_to_string(image)
|
28 |
+
else:
|
29 |
+
return f"Unsupported file type: {file.name}"
|
30 |
+
|
31 |
+
# Simulate passing the content to the phi-4 model
|
32 |
+
model_response = f"Processed file content:\n{content}"
|
33 |
+
return model_response
|
34 |
|
35 |
with gr.Blocks(fill_height=True) as demo:
|
36 |
with gr.Sidebar():
|
|
|
39 |
button = gr.LoginButton("Sign in")
|
40 |
|
41 |
with gr.Column():
|
42 |
+
# Load the phi-4 model
|
43 |
+
model = gr.load("models/microsoft/phi-4", accept_token=button, provider="nebius")
|
44 |
+
|
45 |
+
# File upload component
|
46 |
+
file_input = gr.File(label="Upload a file (TXT, PDF, or Image)")
|
47 |
+
|
48 |
+
# Output component to display model response
|
49 |
+
file_output = gr.Textbox(label="Model Response", lines=10)
|
50 |
|
51 |
+
# Connect the file upload to the processing function
|
52 |
file_input.change(process_file, inputs=file_input, outputs=file_output)
|
53 |
|
54 |
demo.launch()
|