Yermek68 commited on
Commit
39f1ae7
·
verified ·
1 Parent(s): fe95bf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -24
app.py CHANGED
@@ -4,14 +4,13 @@ from fastapi import FastAPI
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from langdetect import detect
6
  from functools import lru_cache
7
- import asyncio
8
  import re
9
 
10
  # ======================================================
11
- # 🚀 Eroha Summarizer PRO++++ v2.1 (by Yermek68)
12
  # ======================================================
13
 
14
- # Кэш пайплайнов
15
  @lru_cache(maxsize=10)
16
  def get_summarizer(lang: str, long: bool = False):
17
  if lang == "ru":
@@ -34,10 +33,7 @@ def get_sentiment_analyzer(lang: str):
34
  model = "j-hartmann/emotion-english-distilroberta-base"
35
  return pipeline("text-classification", model=model, top_k=None)
36
 
37
- # ======================================
38
- # 🧠 Вспомогательные функции
39
- # ======================================
40
-
41
  def clean_text(text: str) -> str:
42
  text = re.sub(r"[^\x00-\x7Fа-яА-ЯёЁ.,!?;:\-–—«»\"'()\[\] ]", "", text)
43
  text = text.replace("▁", " ").replace("<n>", "\n").replace("<s>", "").replace("</s>", "")
@@ -71,10 +67,9 @@ def detect_genre(text: str):
71
  return "📄 Текст общего типа"
72
 
73
  # =====================================================
74
- # 🧩 Основная функция суммаризации
75
  # =====================================================
76
-
77
- async def summarize_text(text: str):
78
  if not text.strip():
79
  return "❌ Введите текст для анализа."
80
 
@@ -94,13 +89,11 @@ async def summarize_text(text: str):
94
  summary = text
95
  else:
96
  max_len, min_len = (250, 60) if words > 300 else (120, 40)
97
- loop = asyncio.get_event_loop()
98
- summary_raw = await loop.run_in_executor(None, lambda: summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"])
99
  summary = clean_text(summary_raw)
100
 
101
  # Анализ эмоций
102
- loop = asyncio.get_event_loop()
103
- emotions = await loop.run_in_executor(None, lambda: sentiment_model(summary))
104
  emo_label = emotions[0]["label"]
105
  emo_score = emotions[0].get("score", 0)
106
 
@@ -121,7 +114,7 @@ async def summarize_text(text: str):
121
  color = "green" if "Радость" in emotion else "red" if "Грусть" in emotion or "Гнев" in emotion else "orange"
122
 
123
  formatted_output = f"""
124
- # 🧠 <span style='color:#0073e6'>Eroha Summarizer PRO++++ v2.1</span>
125
  _(Автоязык: {'Русский' if lang == 'ru' else 'Английский'})_
126
 
127
  ---
@@ -150,31 +143,31 @@ _(Автоязык: {'Русский' if lang == 'ru' else 'Английский
150
  # 🌐 FastAPI backend
151
  # =====================================================
152
 
153
- app = FastAPI(title="Eroha Summarizer PRO++++ v2.1", version="2.1")
154
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
155
 
156
  @app.post("/api/full")
157
- async def api_full(data: dict):
158
  text = data.get("text", "")
159
- summary = await summarize_text(text)
160
  return {"summary": summary}
161
 
162
  @app.post("/api/lite")
163
- async def api_lite(data: dict):
164
  text = data.get("text", "")
165
- result = await summarize_text(text)
166
  clean_result = re.sub(r"<[^>]+>", "", result)
167
  return {"tldr": clean_result[:300]}
168
 
169
  # =====================================================
170
- # 🎨 Gradio интерфейс с кнопками
171
  # =====================================================
172
 
173
  def gradio_summary(text):
174
- return asyncio.run(summarize_text(text))
175
 
176
- with gr.Blocks(title="Eroha Summarizer PRO++++ v2.1") as iface:
177
- gr.Markdown("## 🧠 Eroha Summarizer PRO++++ v2.1\nAI-инструмент нового поколения для анализа, темы, эмоций и автоопределения языка (рус/англ/нем/исп/фр).")
178
 
179
  text_input = gr.Textbox(lines=10, label="Введите текст для анализа и суммаризации")
180
  result_output = gr.Markdown(label="Результат")
 
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from langdetect import detect
6
  from functools import lru_cache
 
7
  import re
8
 
9
  # ======================================================
10
+ # 🚀 Eroha Summarizer PRO++++ v2.1.1 Stable (by Yermek68)
11
  # ======================================================
12
 
13
+ # --- Кэш моделей ---
14
  @lru_cache(maxsize=10)
15
  def get_summarizer(lang: str, long: bool = False):
16
  if lang == "ru":
 
33
  model = "j-hartmann/emotion-english-distilroberta-base"
34
  return pipeline("text-classification", model=model, top_k=None)
35
 
36
+ # --- Вспомогательные функции ---
 
 
 
37
  def clean_text(text: str) -> str:
38
  text = re.sub(r"[^\x00-\x7Fа-яА-ЯёЁ.,!?;:\-–—«»\"'()\[\] ]", "", text)
39
  text = text.replace("▁", " ").replace("<n>", "\n").replace("<s>", "").replace("</s>", "")
 
67
  return "📄 Текст общего типа"
68
 
69
  # =====================================================
70
+ # 🧩 Основная функция
71
  # =====================================================
72
+ def summarize_text(text: str):
 
73
  if not text.strip():
74
  return "❌ Введите текст для анализа."
75
 
 
89
  summary = text
90
  else:
91
  max_len, min_len = (250, 60) if words > 300 else (120, 40)
92
+ summary_raw = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)[0]["summary_text"]
 
93
  summary = clean_text(summary_raw)
94
 
95
  # Анализ эмоций
96
+ emotions = sentiment_model(summary)
 
97
  emo_label = emotions[0]["label"]
98
  emo_score = emotions[0].get("score", 0)
99
 
 
114
  color = "green" if "Радость" in emotion else "red" if "Грусть" in emotion or "Гнев" in emotion else "orange"
115
 
116
  formatted_output = f"""
117
+ # 🧠 <span style='color:#0073e6'>Eroha Summarizer PRO++++ v2.1.1 Stable</span>
118
  _(Автоязык: {'Русский' if lang == 'ru' else 'Английский'})_
119
 
120
  ---
 
143
  # 🌐 FastAPI backend
144
  # =====================================================
145
 
146
+ app = FastAPI(title="Eroha Summarizer PRO++++ v2.1.1", version="2.1.1")
147
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
148
 
149
  @app.post("/api/full")
150
+ def api_full(data: dict):
151
  text = data.get("text", "")
152
+ summary = summarize_text(text)
153
  return {"summary": summary}
154
 
155
  @app.post("/api/lite")
156
+ def api_lite(data: dict):
157
  text = data.get("text", "")
158
+ result = summarize_text(text)
159
  clean_result = re.sub(r"<[^>]+>", "", result)
160
  return {"tldr": clean_result[:300]}
161
 
162
  # =====================================================
163
+ # 🎨 Gradio интерфейс
164
  # =====================================================
165
 
166
  def gradio_summary(text):
167
+ return summarize_text(text)
168
 
169
+ with gr.Blocks(title="Eroha Summarizer PRO++++ v2.1.1") as iface:
170
+ gr.Markdown("## 🧠 Eroha Summarizer PRO++++ v2.1.1 Stable\nAI-инструмент нового поколения для анализа, темы, эмоций и автоопределения языка (рус/англ/нем/исп/фр).")
171
 
172
  text_input = gr.Textbox(lines=10, label="Введите текст для анализа и суммаризации")
173
  result_output = gr.Markdown(label="Результат")