File size: 1,776 Bytes
7bf655d
616b287
7bf655d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
616b287
 
7bf655d
 
 
 
 
 
 
 
 
 
 
 
5209b6b
 
 
 
7bf655d
 
5209b6b
 
7bf655d
 
 
 
 
5209b6b
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel
import fitz  # PyMuPDF for PDF handling

# Function to extract text from PDF
def extract_text_from_pdf(pdf_path):
    doc = fitz.open(pdf_path)
    text = ""
    for page in doc:
        text += page.get_text()
    return text

# Function to handle file upload and text input
def analyze_document(file, prompt):
    # Check file type and extract text accordingly
    if file.name.endswith(".pdf"):
        text = extract_text_from_pdf(file.name)
    elif file.name.endswith(".txt"):
        text = file.read().decode("utf-8")
    else:
        return "Unsupported file format. Please upload a PDF or TXT file."
    
    # Load model and tokenizer
    # model_name = "Alibaba-NLP/gte-Qwen1.5-7B-instruct"
    model_name = "THUDM/glm-4-9b"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(model_name)
    
    # Generate input for the model
    input_text = f"Document content:\n{text}\n\nPrompt:\n{prompt}"
    inputs = tokenizer(input_text, return_tensors="pt")
    outputs = model.generate(**inputs)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return response

# Define Gradio interface
file_input = gr.File(label="Upload TXT or PDF Document", file_count="single")
prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your structured prompt here")
output_text = gr.Textbox(label="Analysis Result")

iface = gr.Interface(
    fn=analyze_document,
    inputs=[file_input, prompt_input],
    outputs=output_text,
    title="Document Analysis with GPT Model",
    description="Upload a TXT or PDF document and enter a prompt to get an analysis."
)

# Launch the interface
iface.launch()