Asanaly commited on
Commit
7b34f32
·
verified ·
1 Parent(s): 8c02922

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -40
app.py CHANGED
@@ -1,42 +1,34 @@
1
- import streamlit as st
2
  from transformers import pipeline
3
 
4
- st.set_page_config(page_title="Emotion Analyzer", page_icon="🙂")
5
-
6
- st.title("Twitter Sentiment Analysis (BERT)")
7
- st.write("Твиттердің эмоционалды реңкін анықтайтын веб-қосымша")
8
-
9
- # Load model
10
- @st.cache_resource
11
- def load_model():
12
- return pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
13
-
14
- model = load_model()
15
-
16
- # User input
17
- text = st.text_area("Мәтінді енгізіңіз:", placeholder="Мысалы: Бұл жоба өте керемет!")
18
-
19
- if st.button("Анализ жасау"):
20
- if text.strip() == "":
21
- st.warning("Мәтін енгізіңіз!")
22
- else:
23
- result = model(text)[0]
24
-
25
- label = result["label"]
26
- score = result["score"]
27
-
28
- st.subheader("Нәтиже:")
29
- st.write(f"**Эмоция:** {label}")
30
- st.write(f"**Дәлдік:** {round(score, 3)}")
31
-
32
- # Simple emotion label mapping
33
- if label == "1 star":
34
- st.error("Негатив 😠")
35
- elif label == "2 stars":
36
- st.warning("Соншалықты жақсы емес 😕")
37
- elif label == "3 stars":
38
- st.info("Нейтрал 🙂")
39
- elif label == "4 stars":
40
- st.success("Жақсы 🙂")
41
- elif label == "5 stars":
42
- st.success("Өте позитивті 😍")
 
1
+ import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load model once (cached by HF)
5
+ sentiment_model = pipeline(
6
+ "sentiment-analysis",
7
+ model="nlptown/bert-base-multilingual-uncased-sentiment"
8
+ )
9
+
10
+ def analyze(text):
11
+ result = sentiment_model(text)[0]
12
+ label = result["label"]
13
+ score = round(result["score"], 3)
14
+
15
+ mapping = {
16
+ "1 star": "Өте негатив 😡",
17
+ "2 stars": "Негатив 😠",
18
+ "3 stars": "Нейтрал 🙂",
19
+ "4 stars": "Позитив 😊",
20
+ "5 stars": "Өте позитив 😍",
21
+ }
22
+
23
+ emotion = mapping.get(label, label)
24
+ return f"Эмоция: {emotion}\nДәлдік: {score}"
25
+
26
+ ui = gr.Interface(
27
+ fn=analyze,
28
+ inputs=gr.Textbox(label="Мәтін енгізіңіз"),
29
+ outputs=gr.Textbox(label="Нәтиже"),
30
+ title="Sentiment Analysis",
31
+ description="BERT моделіне негізделген эмоция талдау"
32
+ )
33
+
34
+ ui.launch()