Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,65 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
from
|
5 |
-
import
|
6 |
-
import
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
text = ""
|
18 |
-
|
19 |
-
|
20 |
-
text += pytesseract.image_to_string(page)
|
21 |
return text
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
-
|
29 |
-
def process_image(image):
|
30 |
-
return pytesseract.image_to_string(image)
|
31 |
-
|
32 |
-
# Main function that handles all file types
|
33 |
-
def handle_files(file):
|
34 |
if file.name.endswith('.pdf'):
|
35 |
-
text =
|
36 |
elif file.name.endswith('.csv'):
|
37 |
text = process_csv(file)
|
38 |
else:
|
39 |
-
|
40 |
-
text = process_image(image)
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
)
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import PyPDF2
|
5 |
+
import csv
|
6 |
+
import io
|
7 |
|
8 |
+
# Load the model and tokenizer
|
9 |
+
model_name = "your_fine_tuned_model_name" # Replace with your actual model name
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
12 |
|
13 |
+
def process_text(text):
|
14 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
15 |
+
with torch.no_grad():
|
16 |
+
outputs = model.generate(**inputs, max_length=1000)
|
17 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
|
19 |
+
def extract_text_from_pdf(file):
|
20 |
+
pdf_reader = PyPDF2.PdfFileReader(file)
|
21 |
text = ""
|
22 |
+
for page in range(pdf_reader.numPages):
|
23 |
+
text += pdf_reader.getPage(page).extractText()
|
|
|
24 |
return text
|
25 |
|
26 |
+
def process_csv(file):
|
27 |
+
content = file.read().decode('utf-8')
|
28 |
+
csv_reader = csv.reader(io.StringIO(content))
|
29 |
+
rows = list(csv_reader)
|
30 |
+
return "\n".join([",".join(row) for row in rows])
|
31 |
|
32 |
+
def analyze_document(file):
|
|
|
|
|
|
|
|
|
|
|
33 |
if file.name.endswith('.pdf'):
|
34 |
+
text = extract_text_from_pdf(file)
|
35 |
elif file.name.endswith('.csv'):
|
36 |
text = process_csv(file)
|
37 |
else:
|
38 |
+
return "Unsupported file format. Please upload a PDF or CSV file."
|
|
|
39 |
|
40 |
+
prompt = f"Analyze the following procurement document and provide a detailed audit report:\n\n{text}"
|
41 |
+
return process_text(prompt)
|
42 |
+
|
43 |
+
def answer_question(question, context):
|
44 |
+
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
|
45 |
+
return process_text(prompt)
|
46 |
+
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
gr.Markdown("# AuditBidden: AI-Powered Public Procurement Auditor")
|
49 |
|
50 |
+
with gr.Tab("Document Analysis"):
|
51 |
+
file_input = gr.File(label="Upload Procurement Document (PDF or CSV)")
|
52 |
+
analyze_button = gr.Button("Analyze Document")
|
53 |
+
analysis_output = gr.Textbox(label="Audit Report")
|
54 |
+
|
55 |
+
analyze_button.click(analyze_document, inputs=file_input, outputs=analysis_output)
|
56 |
+
|
57 |
+
with gr.Tab("Q&A"):
|
58 |
+
context_input = gr.Textbox(label="Context (paste relevant procurement information)")
|
59 |
+
question_input = gr.Textbox(label="Question")
|
60 |
+
answer_button = gr.Button("Get Answer")
|
61 |
+
answer_output = gr.Textbox(label="Answer")
|
62 |
+
|
63 |
+
answer_button.click(answer_question, inputs=[question_input, context_input], outputs=answer_output)
|
64 |
+
|
65 |
+
demo.launch()
|