Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| import time | |
| # 尝试更强的模型(如 flan-t5-large);如果内存不够,可改回 base | |
| model = pipeline( | |
| "text2text-generation", | |
| model="google/flan-t5-large", | |
| device_map="auto" | |
| ) | |
| def analyze_text(text): | |
| """分析英文原著文本,每句分析时显示进度。""" | |
| if not text.strip(): | |
| yield "⚠️ 请先输入英文文本。" | |
| return | |
| sentences = [s.strip() for s in text.split('.') if s.strip()] | |
| total = len(sentences) | |
| yield f"📖 共检测到 {total} 句,开始分析中...\n" | |
| all_results = [] | |
| for i, sentence in enumerate(sentences, start=1): | |
| prompt = f""" | |
| You are an advanced English literature analysis assistant. | |
| Please analyze the following sentence from a literary perspective. | |
| Explain: | |
| 1. Grammar and sentence structure | |
| 2. Vocabulary richness | |
| 3. Idiomatic/natural usage | |
| 4. Possible literary meaning or tone | |
| Then give a short summary (in English). | |
| Sentence: | |
| "{sentence}" | |
| """ | |
| result = model( | |
| prompt, | |
| max_length=512, | |
| do_sample=True, | |
| temperature=0.7 | |
| )[0]['generated_text'] | |
| all_results.append(f"Sentence {i}:\n{result}\n") | |
| progress = int(i / total * 100) | |
| yield f"⏳ 分析进度:{i}/{total} ({progress}%)\n\n" + "\n".join(all_results) | |
| time.sleep(0.5) | |
| yield f"✅ 分析完成!共 {total} 句。\n\n" + "\n".join(all_results) | |
| # 界面 | |
| demo = gr.Interface( | |
| fn=analyze_text, | |
| inputs=gr.Textbox(label="输入英文原著片段", lines=10, placeholder="例如:It was the best of times, it was the worst of times..."), | |
| outputs=gr.Textbox(label="分析结果", lines=15), | |
| title="📚 英文原著阅读与分析助手", | |
| description="逐句分析英文原著的语法、词汇和文体特征,并实时显示进度。", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |