File size: 1,141 Bytes
e15d7ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from transformers import pipeline
from upload.data import process_uploaded_file, extract_text_from_pdf, extract_key_info

summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")

def summarize_text(text: str) -> str:
    summary = summarizer(text, max_length=50, min_length=10, do_sample=False)
    return summary[0]['summary_text']

def chat_with_document(text: str, user_input: str) -> str:
    from transformers import Conversation
    conversation = Conversation(user_input)
    response = chatbot([conversation], text_context=text)
    return response.generated_responses[-1]

def process_and_analyze(file_path: str):
    data = process_uploaded_file(file_path)
    text = extract_text_from_pdf(file_path)
    
    summary = summarize_text(text)
    print(f"Tóm tắt: {summary}")
    
    while True:
        user_input = input("Nhập câu hỏi (hoặc 'exit' để thoát): ")
        if user_input.lower() == 'exit':
            break
        response = chat_with_document(text, user_input)
        print(f"Trả lời: {response}")