NANCY03 commited on
Commit
61f8da9
·
verified ·
1 Parent(s): edb14cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -2,8 +2,12 @@ import gradio as gr
2
  from transformers import pipeline
3
  import time
4
 
5
- # 加载小一点的模型以提高速度
6
- model = pipeline("text2text-generation", model="google/flan-t5-base")
 
 
 
 
7
 
8
  def analyze_text(text):
9
  """分析英文原著文本,每句分析时显示进度。"""
@@ -18,19 +22,33 @@ def analyze_text(text):
18
 
19
  all_results = []
20
  for i, sentence in enumerate(sentences, start=1):
21
- # 生成prompt
22
- prompt = f"Analyze the following English sentence for vocabulary, grammar, and meaning:\n\n{sentence}\n\nGive a short analysis in English."
23
-
24
- # 调用模型
25
- result = model(prompt, max_length=150)[0]['generated_text']
26
- all_results.append(f"Sentence {i}: {result}")
27
-
28
- # 每次yield一个更新,显示进度
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  progress = int(i / total * 100)
30
- yield f"⏳ 分析进度:{i}/{total} ({progress}%)\n\n" + "\n\n".join(all_results)
31
- time.sleep(0.3) # 轻微延迟让前端看到变化
32
 
33
- yield f"✅ 分析完成!共 {total} 句。\n\n" + "\n\n".join(all_results)
34
 
35
 
36
  # 界面
@@ -38,9 +56,8 @@ demo = gr.Interface(
38
  fn=analyze_text,
39
  inputs=gr.Textbox(label="输入英文原著片段", lines=10, placeholder="例如:It was the best of times, it was the worst of times..."),
40
  outputs=gr.Textbox(label="分析结果", lines=15),
41
- title="📚 英文原著阅读助手",
42
- description="自动逐句分析英文原著中的词汇、语法和含义。支持进度显示。",
43
- live=False
44
  )
45
 
46
  if __name__ == "__main__":
 
2
  from transformers import pipeline
3
  import time
4
 
5
+ # 尝试更强的模型(如 flan-t5-large);如果内存不够,可改回 base
6
+ model = pipeline(
7
+ "text2text-generation",
8
+ model="google/flan-t5-large",
9
+ device_map="auto"
10
+ )
11
 
12
  def analyze_text(text):
13
  """分析英文原著文本,每句分析时显示进度。"""
 
22
 
23
  all_results = []
24
  for i, sentence in enumerate(sentences, start=1):
25
+ prompt = f"""
26
+ You are an advanced English literature analysis assistant.
27
+ Please analyze the following sentence from a literary perspective.
28
+ Explain:
29
+ 1. Grammar and sentence structure
30
+ 2. Vocabulary richness
31
+ 3. Idiomatic/natural usage
32
+ 4. Possible literary meaning or tone
33
+ Then give a short summary (in English).
34
+
35
+ Sentence:
36
+ "{sentence}"
37
+ """
38
+ result = model(
39
+ prompt,
40
+ max_length=512,
41
+ do_sample=True,
42
+ temperature=0.7
43
+ )[0]['generated_text']
44
+
45
+ all_results.append(f"Sentence {i}:\n{result}\n")
46
+
47
  progress = int(i / total * 100)
48
+ yield f"⏳ 分析进度:{i}/{total} ({progress}%)\n\n" + "\n".join(all_results)
49
+ time.sleep(0.5)
50
 
51
+ yield f"✅ 分析完成!共 {total} 句。\n\n" + "\n".join(all_results)
52
 
53
 
54
  # 界面
 
56
  fn=analyze_text,
57
  inputs=gr.Textbox(label="输入英文原著片段", lines=10, placeholder="例如:It was the best of times, it was the worst of times..."),
58
  outputs=gr.Textbox(label="分析结果", lines=15),
59
+ title="📚 英文原著阅读与分析助手",
60
+ description="逐句分析英文原著的语法、词汇和文体特征,并实时显示进度。",
 
61
  )
62
 
63
  if __name__ == "__main__":