firefighter/PdfSumGPT
3
1import gradio as gr2 3from utils.chatgpt import ChatGPTAPI4from utils.read_pdf import read_pdf5from utils.read_web import read_web6from utils.truncate import truncate_string7 8 9def file2str(filepath: str) -> str:10 if not filepath:11 return ''12 if filepath.endswith('.pdf'):13 content_list = read_pdf(filepath)14 text = '\n'.join(content_list)15 elif filepath.endswith('.txt'):16 with open(filepath, 'r') as f:17 text = f.readlines()18 else:19 raise Exception('File type not supported')20 text = truncate_string(text, max_length=1024)21 return text22 23 24def process(api_key: str = '', prompt: str = '', file=None, url='') -> str:25 chatgpt = ChatGPTAPI(api_key, max_input_length=1024)26 27 file_text = file2str(file.name) if file else ''28 web_txt = read_web(url)29 web_txt = truncate_string(web_txt, max_length=1024)30 31 content = prompt + '\n' + file_text + '\n' + web_txt32 response = chatgpt(content)33 return response34 35 36prompt_input = gr.components.Textbox(37 value='用中文总结下面的文章',38 lines=2,39 type="text"40)41 42app = gr.Interface(43 fn=process,44 inputs=["text", prompt_input, "file", "text"],45 outputs="text"46)47app.launch()48 