Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -46,52 +46,5 @@ def main():
|
|
46 |
description="Laden Sie eine PDF-Datei hoch und stellen Sie Fragen zu ihrem Inhalt."
|
47 |
).launch()
|
48 |
|
49 |
-
if __name__ == "__main__":
|
50 |
-
main()
|
51 |
-
import gradio as gr
|
52 |
-
import chromadb
|
53 |
-
import fitz # PyMuPDF
|
54 |
-
import time
|
55 |
-
|
56 |
-
client = chromadb.PersistentClient(path="./")
|
57 |
-
collection = client.get_or_create_collection(name="code")
|
58 |
-
|
59 |
-
# Function to extract text from PDF file
|
60 |
-
def extract_text_from_pdf(file_path):
|
61 |
-
try:
|
62 |
-
doc = fitz.open(file_path)
|
63 |
-
text = ""
|
64 |
-
for page in doc:
|
65 |
-
text += page.get_text()
|
66 |
-
return text
|
67 |
-
except Exception as e:
|
68 |
-
return f"Error occurred while reading PDF file: {e}"
|
69 |
-
|
70 |
-
|
71 |
-
def process_pdf(uploaded_file, prompt):
|
72 |
-
if uploaded_file is not None:
|
73 |
-
# Extract text from uploaded PDF file
|
74 |
-
pdf_text = extract_text_from_pdf(uploaded_file.name)
|
75 |
-
timestamp = time.time()
|
76 |
-
if pdf_text:
|
77 |
-
collection.add(
|
78 |
-
documents=[pdf_text],
|
79 |
-
ids=[timestamp]
|
80 |
-
|
81 |
-
)
|
82 |
-
print(pdf_text)
|
83 |
-
outputs=pdf_text
|
84 |
-
return pdf_text
|
85 |
-
|
86 |
-
gr.Interface(
|
87 |
-
fn=process_pdf,
|
88 |
-
inputs=[gr.File(type="filepath", label="Upload PDF File"),
|
89 |
-
gr.Textbox(lines=2, placeholder="Ask a Question")],
|
90 |
-
outputs="text",
|
91 |
-
title="PDF Chatbot",
|
92 |
-
description="Upload a PDF file and ask questions about its content."
|
93 |
-
).launch()
|
94 |
-
|
95 |
-
|
96 |
if __name__ == "__main__":
|
97 |
main()
|
|
|
46 |
description="Laden Sie eine PDF-Datei hoch und stellen Sie Fragen zu ihrem Inhalt."
|
47 |
).launch()
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if __name__ == "__main__":
|
50 |
main()
|