File size: 2,407 Bytes
50764c7
ec854f5
 
 
 
 
50764c7
ec854f5
 
 
 
a7a1018
ec854f5
 
 
 
 
50764c7
ec854f5
 
50764c7
ec854f5
 
50764c7
 
ec854f5
 
 
 
 
50764c7
ec854f5
50764c7
ec854f5
50764c7
 
 
ec854f5
9f4520a
ec854f5
 
 
 
 
 
 
 
 
9f4520a
ec854f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import PyPDF2
import csv
import io

# Load the model and tokenizer
model_name = "your_fine_tuned_model_name"  # Replace with your actual model name
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

def process_text(text):
    inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
    with torch.no_grad():
        outputs = model.generate(**inputs, max_length=1000)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

def extract_text_from_pdf(file):
    pdf_reader = PyPDF2.PdfFileReader(file)
    text = ""
    for page in range(pdf_reader.numPages):
        text += pdf_reader.getPage(page).extractText()
    return text

def process_csv(file):
    content = file.read().decode('utf-8')
    csv_reader = csv.reader(io.StringIO(content))
    rows = list(csv_reader)
    return "\n".join([",".join(row) for row in rows])

def analyze_document(file):
    if file.name.endswith('.pdf'):
        text = extract_text_from_pdf(file)
    elif file.name.endswith('.csv'):
        text = process_csv(file)
    else:
        return "Unsupported file format. Please upload a PDF or CSV file."
    
    prompt = f"Analyze the following procurement document and provide a detailed audit report:\n\n{text}"
    return process_text(prompt)

def answer_question(question, context):
    prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
    return process_text(prompt)

with gr.Blocks() as demo:
    gr.Markdown("# AuditBidden: AI-Powered Public Procurement Auditor")
    
    with gr.Tab("Document Analysis"):
        file_input = gr.File(label="Upload Procurement Document (PDF or CSV)")
        analyze_button = gr.Button("Analyze Document")
        analysis_output = gr.Textbox(label="Audit Report")
        
        analyze_button.click(analyze_document, inputs=file_input, outputs=analysis_output)
    
    with gr.Tab("Q&A"):
        context_input = gr.Textbox(label="Context (paste relevant procurement information)")
        question_input = gr.Textbox(label="Question")
        answer_button = gr.Button("Get Answer")
        answer_output = gr.Textbox(label="Answer")
        
        answer_button.click(answer_question, inputs=[question_input, context_input], outputs=answer_output)

demo.launch()