kimappl commited on
Commit
924e5b8
Β·
verified Β·
1 Parent(s): f4b8bb3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ # Set up the page configuration for a wide layout and better experience
5
+ st.set_page_config(
6
+ page_title="Live Transformer Demo",
7
+ layout="wide", # This will allow a wider layout for your app
8
+ )
9
+
10
+ # Add custom CSS for the header background
11
+ st.markdown("""
12
+ <style>
13
+ .header {
14
+ background-image: url('https://images.unsplash.com/photo-1557682250-48bfe2db9041');
15
+ background-size: cover;
16
+ padding: 60px;
17
+ text-align: center;
18
+ border-radius: 15px;
19
+ color: white;
20
+ font-family: 'Arial', sans-serif;
21
+ }
22
+ .header h1 {
23
+ font-size: 50px;
24
+ font-weight: bold;
25
+ }
26
+ .header p {
27
+ font-size: 20px;
28
+ margin-top: 10px;
29
+ }
30
+ .header a {
31
+ color: #ffcc00;
32
+ font-weight: bold;
33
+ text-decoration: none;
34
+ }
35
+ .header a:hover {
36
+ text-decoration: underline;
37
+ }
38
+ </style>
39
+ <div class="header">
40
+ <h1>πŸ€— Live Transformer Demo</h1>
41
+ <p>Explore Sentiment Analysis and Translation using models from <a href="https://huggingface.co/" target="_blank">Hugging Face</a>.</p>
42
+ </div>
43
+ """, unsafe_allow_html=True)
44
+
45
+ # Add an explanation of the app with markdown
46
+ st.markdown("""
47
+ Welcome to the Transformer NLP Demo! This app showcases **Sentiment Analysis** and **Translation** tasks.
48
+
49
+ - πŸ” **Sentiment Analysis** for understanding opinions.
50
+ - 🌐 **Translation** across multiple languages, including Albanian, Dutch, French, German, Hindi, Indonesian, Italian, Mandarin (Chinese), Russian, and Spanish.
51
+
52
+ Simply choose a task below, enter your text, and click 'Run' to see the results!
53
+ """)
54
+
55
+ # Two-column layout
56
+ col1, col2 = st.columns([2, 1])
57
+
58
+ # Left column for input and task selection
59
+ with col1:
60
+ st.subheader("Start Exploring")
61
+
62
+ # Task selection
63
+ task = st.selectbox("Choose a task", ["Sentiment Analysis", "Translation"])
64
+
65
+ # Language selection for translation
66
+ target_language = None
67
+ if task == "Translation":
68
+ target_language = st.selectbox("Select language", [
69
+ "Albanian", "Dutch", "French", "German", "Hindi", "Indonesian",
70
+ "Italian", "Mandarin (Chinese)", "Russian", "Spanish"
71
+ ])
72
+
73
+ # Text input from the user
74
+ user_input = st.text_area("Enter your text here:", height=150)
75
+
76
+ # Load the appropriate model with advanced caching
77
+ @st.cache_resource(ttl=24*3600, max_entries=10)
78
+ def load_model(task_name, target_language=None):
79
+ if task_name == "Sentiment Analysis":
80
+ return pipeline("sentiment-analysis")
81
+ elif task_name == "Translation":
82
+ if target_language == "Albanian":
83
+ return pipeline("translation_en_to_sq", model="Helsinki-NLP/opus-mt-en-sq")
84
+ elif target_language == "Dutch":
85
+ return pipeline("translation_en_to_nl", model="Helsinki-NLP/opus-mt-en-nl")
86
+ elif target_language == "French":
87
+ return pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")
88
+ elif target_language == "German":
89
+ return pipeline("translation_en_to_de", model="Helsinki-NLP/opus-mt-en-de")
90
+ elif target_language == "Hindi":
91
+ return pipeline("translation_en_to_hi", model="Helsinki-NLP/opus-mt-en-hi")
92
+ elif target_language == "Indonesian":
93
+ return pipeline("translation_en_to_id", model="Helsinki-NLP/opus-mt-en-id")
94
+ elif target_language == "Italian":
95
+ return pipeline("translation_en_to_it", model="Helsinki-NLP/opus-mt-en-it")
96
+ elif target_language == "Mandarin (Chinese)":
97
+ return pipeline("translation_en_to_zh", model="Helsinki-NLP/opus-mt-en-zh")
98
+ elif target_language == "Russian":
99
+ return pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru")
100
+ elif target_language == "Spanish":
101
+ return pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es")
102
+
103
+ model = load_model(task, target_language)
104
+
105
+ # Cache the results of each task to avoid re-computation
106
+ @st.cache_data(ttl=24*3600, max_entries=50)
107
+ def analyze_sentiment(input_text):
108
+ return model(input_text)
109
+
110
+ @st.cache_data(ttl=24*3600, max_entries=50)
111
+ def translate_text(input_text):
112
+ return model(input_text)[0]["translation_text"]
113
+
114
+ # Perform the selected task
115
+ if st.button("Run"):
116
+ if user_input:
117
+ with st.spinner(f"Performing {task.lower()}..."):
118
+ if task == "Sentiment Analysis":
119
+ result = analyze_sentiment(user_input)[0]
120
+ label = result["label"]
121
+ score = result["score"]
122
+ st.success("Sentiment Analysis Result:")
123
+ st.write(f"**Label**: {label}, **Score**: {score:.4f}")
124
+ elif task == "Translation":
125
+ translation = translate_text(user_input)
126
+ st.success(f"Translation (English to {target_language}):")
127
+ st.write(translation)
128
+ else:
129
+ st.error("Please enter some text.")