Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,12 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
model = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
progress = int(i / total * 100)
|
| 30 |
-
yield f"⏳ 分析进度:{i}/{total} ({progress}%)\n\n" + "\n
|
| 31 |
-
time.sleep(0.
|
| 32 |
|
| 33 |
-
yield f"✅ 分析完成!共 {total} 句。\n\n" + "\n
|
| 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__":
|