sashadd commited on
Commit
a3c6681
·
verified ·
1 Parent(s): 39470a0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ TASK = sentiment-analysis
6
+ MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment-latest"
7
+
8
+ pipe = pipeline(TASK, model = MODEL_NAME)
9
+
10
+ MAX_CHARS = 2000
11
+
12
+ def run (text:str):
13
+ if (text is None or not text.strip()):
14
+ return "Ошибка: введено пустое значение!", None, None
15
+
16
+ text = text.strip()
17
+ if (len(text) > MAX_CHARS):
18
+ text = text[:MAX_CHARS]
19
+
20
+ t0 = time.time()
21
+
22
+ try:
23
+ result = pipe(text)
24
+ latency = round((time.time() - t0) * 1000, 1)
25
+ return "OK", result, f"{latency} ms"
26
+ except Exception as e:
27
+ return f"Ошибка: {type(e).name}: {e}", None, None
28
+
29
+ with gr.Blocks() as demo:
30
+ gr.Markdown(f"""
31
+ # NLP-приложение (Hugging Face Spaces + Gradio)
32
+ Задача: {TASK}
33
+ Модель: {MODEL_NAME}
34
+ """)
35
+
36
+ inp = gr.Textbox(label = "Введите текст", lines = 6, placeholder = "Скопируйте текст")
37
+ btn = gr.Button("Обработать")
38
+ status = gr.Textbox(label = "Статус")
39
+ out = gr.JSON(label = "Результат модели")
40
+ latency = gr.Textbox(label = "Время ответа")
41
+
42
+ btn.click(run, inputs = inp, outputs = [status, out, latency])
43
+ gr.Examples(
44
+ examples=[
45
+ ["I love this product! It works great."],
46
+ ["This is the worst experience ever."],
47
+ ["It's okay, nothing special."]
48
+ ],
49
+ inputs=inp
50
+ )
51
+
52
+ demo.launch()
53
+
54
+
55
+
56
+