Ericahooooo commited on
Commit
43587d2
·
verified ·
1 Parent(s): 6ab706f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -23
app.py CHANGED
@@ -1,24 +1,21 @@
1
- import gradio as gr
2
- import os
3
 
4
- model = gr.load("models/fnlp/bart-base-chinese")
 
5
 
6
  def generate_summary(file):
 
 
 
7
  # 读取上传的文本文件内容
8
- with open(file.name, 'r', encoding='utf-8') as f:
9
- text_content = f.read()
10
- print("File Content:", text_content) # 添加此行进行调试
11
 
12
- # 使用模型进行处理(在这里是摘要生成,你可以替换为你的具体模型处理逻辑)
13
- result = model.predict(text_content)
14
-
15
- return result
16
 
17
- def read_file_content(file):
18
- # 读取上传的文本文件内容
19
- with open(file.name, 'r', encoding='utf-8') as f:
20
- text_content = f.read()
21
- return text_content
22
 
23
  demo = gr.Interface(
24
  fn=generate_summary,
@@ -27,13 +24,5 @@ demo = gr.Interface(
27
  live=False
28
  )
29
 
30
- # 增加一个上传文件后读取文件内容的接口
31
- file_reader = gr.Interface(
32
- fn=read_file_content,
33
- inputs=gr.File(),
34
- outputs="text",
35
- live=False
36
- )
37
-
38
  # 启动应用
39
  demo.launch(share=True)
 
1
+ import gradio as gr, os
2
+ from transformers import BartForConditionalGeneration
3
 
4
+ # 加载 BART 模型
5
+ model = BartForConditionalGeneration.from_pretrained("models/fnlp/bart-base-chinese")
6
 
7
  def generate_summary(file):
8
+ # 重置文件指针位置
9
+ file.seek(0)
10
+
11
  # 读取上传的文本文件内容
12
+ text_content = file.read()
 
 
13
 
14
+ # 使用模型进行处理(摘要生成)
15
+ summary_ids = model.generate(text_content, max_length=150, min_length=50, length_penalty=2.0, num_beams=4, early_stopping=True)
16
+ summary = model.decode(summary_ids[0], skip_special_tokens=True)
 
17
 
18
+ return summary
 
 
 
 
19
 
20
  demo = gr.Interface(
21
  fn=generate_summary,
 
24
  live=False
25
  )
26
 
 
 
 
 
 
 
 
 
27
  # 启动应用
28
  demo.launch(share=True)