Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,11 @@ import streamlit as st
|
|
3 |
import requests
|
4 |
import json
|
5 |
from PIL import Image
|
6 |
-
import matplotlib.pyplot as plt
|
7 |
-
import numpy as np
|
8 |
from io import BytesIO
|
9 |
|
|
|
|
|
|
|
10 |
# Load tokens from environment variables
|
11 |
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
12 |
GROQ_TOKEN = os.getenv("GROQ_TOKEN")
|
@@ -56,42 +57,157 @@ def generate_text(prompt, max_tokens, temperature):
|
|
56 |
|
57 |
# Process input data
|
58 |
def process_input(tamil_text, max_tokens, temperature):
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
return english_text, image, generated_story
|
63 |
|
64 |
-
#
|
65 |
-
st.markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
st.sidebar.header("Settings")
|
68 |
-
tamil_text_input = st.sidebar.text_area("Enter Tamil Text")
|
69 |
-
max_tokens_input = st.sidebar.slider("Max Tokens", min_value=50, max_value=200, value=100, step=10)
|
70 |
-
temperature_input = st.sidebar.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.1)
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
if st.sidebar.button("Submit"):
|
|
|
73 |
english_text, image, generated_story = process_input(tamil_text_input, max_tokens_input, temperature_input)
|
|
|
74 |
|
75 |
-
|
76 |
-
st.
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
plt.imshow(np.array(image))
|
81 |
-
plt.axis('off')
|
82 |
-
st.pyplot(plt)
|
83 |
-
else:
|
84 |
-
st.error(image)
|
85 |
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import requests
|
4 |
import json
|
5 |
from PIL import Image
|
|
|
|
|
6 |
from io import BytesIO
|
7 |
|
8 |
+
# Set page configuration
|
9 |
+
st.set_page_config(page_title="multilingual-storyteller", layout="wide")
|
10 |
+
|
11 |
# Load tokens from environment variables
|
12 |
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
13 |
GROQ_TOKEN = os.getenv("GROQ_TOKEN")
|
|
|
57 |
|
58 |
# Process input data
|
59 |
def process_input(tamil_text, max_tokens, temperature):
|
60 |
+
with st.spinner("Translating and generating content..."):
|
61 |
+
english_text = translate_tamil_to_english(tamil_text)
|
62 |
+
image = generate_image(english_text)
|
63 |
+
generated_story = generate_text(english_text, max_tokens, temperature)
|
64 |
return english_text, image, generated_story
|
65 |
|
66 |
+
# Style for title with animated gradient
|
67 |
+
st.markdown("""
|
68 |
+
<style>
|
69 |
+
@keyframes gradient {
|
70 |
+
0% {background-position: 0% 50%;}
|
71 |
+
50% {background-position: 100% 50%;}
|
72 |
+
100% {background-position: 0% 50%;}
|
73 |
+
}
|
74 |
+
.title {
|
75 |
+
font-size: 60px;
|
76 |
+
color: white;
|
77 |
+
text-align: center;
|
78 |
+
background: linear-gradient(-45deg, #ff9966, #ff5e62, #ff9966, #ff5e62);
|
79 |
+
background-size: 400% 400%;
|
80 |
+
animation: gradient 10s ease infinite;
|
81 |
+
padding: 20px;
|
82 |
+
border-radius: 12px;
|
83 |
+
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.3);
|
84 |
+
margin-bottom: 20px; /* Space below the title */
|
85 |
+
}
|
86 |
+
</style>
|
87 |
+
""", unsafe_allow_html=True)
|
88 |
+
|
89 |
+
# Main Title
|
90 |
+
st.markdown("<h1 class='title'>multilingual-storyteller</h1>", unsafe_allow_html=True)
|
91 |
+
|
92 |
+
# Sidebar Styling
|
93 |
+
st.markdown("""
|
94 |
+
<style>
|
95 |
+
.sidebar .sidebar-content {
|
96 |
+
background-color: #2c3e50;
|
97 |
+
color: white;
|
98 |
+
text-align: center; /* Center text in sidebar */
|
99 |
+
}
|
100 |
+
.sidebar .stTextArea, .sidebar .stSlider {
|
101 |
+
width: 100%; /* Full width for sidebar elements */
|
102 |
+
}
|
103 |
+
.sidebar .stButton > button {
|
104 |
+
display: block; /* Center buttons */
|
105 |
+
margin: 0 auto; /* Center buttons */
|
106 |
+
}
|
107 |
+
</style>
|
108 |
+
""", unsafe_allow_html=True)
|
109 |
|
110 |
st.sidebar.header("Settings")
|
|
|
|
|
|
|
111 |
|
112 |
+
# Sidebar input elements
|
113 |
+
tamil_text_input = st.sidebar.text_area("Enter Tamil Text", help="Paste or type the Tamil text to translate and generate a story.")
|
114 |
+
max_tokens_input = st.sidebar.slider("Max Tokens", min_value=50, max_value=200, value=100, step=10, help="Controls the length of the generated story.")
|
115 |
+
temperature_input = st.sidebar.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.1, help="Controls the randomness of the generated story.")
|
116 |
+
|
117 |
+
# Initialize the session state for instructions
|
118 |
+
if "show_instructions" not in st.session_state:
|
119 |
+
st.session_state.show_instructions = False
|
120 |
+
|
121 |
+
# How to Use Button
|
122 |
+
if st.sidebar.button("How to Use"):
|
123 |
+
st.session_state.show_instructions = not st.session_state.show_instructions
|
124 |
+
|
125 |
+
# Show instructions if toggled on
|
126 |
+
if st.session_state.show_instructions:
|
127 |
+
st.sidebar.info(
|
128 |
+
"""
|
129 |
+
**Instructions:**
|
130 |
+
1. Enter the Tamil text you want to translate in the text area.
|
131 |
+
2. Adjust the **Max Tokens** slider to set the desired length of the generated story.
|
132 |
+
3. Use the **Temperature** slider to control the creativity of the generated output.
|
133 |
+
4. Click the **Submit** button to get the translated text and generated story along with an image.
|
134 |
+
5. Review the outputs displayed in the main area.
|
135 |
+
"""
|
136 |
+
)
|
137 |
+
|
138 |
+
# Display progress bar
|
139 |
+
progress_bar = st.sidebar.progress(0)
|
140 |
+
|
141 |
+
# Submit Button
|
142 |
if st.sidebar.button("Submit"):
|
143 |
+
progress_bar.progress(20) # Start progress
|
144 |
english_text, image, generated_story = process_input(tamil_text_input, max_tokens_input, temperature_input)
|
145 |
+
progress_bar.progress(100) # Complete progress
|
146 |
|
147 |
+
# Notify the user that the process was successful
|
148 |
+
st.sidebar.success("Translation and generation complete!")
|
149 |
|
150 |
+
# Layout with 2 columns
|
151 |
+
col1, col2 = st.columns([1, 2])
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
+
# Column 1: Translated and Generated Text
|
154 |
+
with col1:
|
155 |
+
st.markdown(f"""
|
156 |
+
<div style="border: 2px solid #ff9966; padding: 15px; border-radius: 10px; background-color: #ffffff;">
|
157 |
+
<h3 style="color: #ff5e62;">Translated English Text</h3>
|
158 |
+
<p style="padding: 10px; background-color: #f9f9f9; border-radius: 10px;">{english_text}</p>
|
159 |
+
</div>
|
160 |
+
""", unsafe_allow_html=True)
|
161 |
|
162 |
+
st.markdown(f"""
|
163 |
+
<div style="border: 2px solid #ff9966; padding: 15px; margin-top: 20px; border-radius: 10px; background-color: #ffffff;">
|
164 |
+
<h3 style="color: #ff5e62;">Generated Story</h3>
|
165 |
+
<p style="padding: 10px; background-color: #f9f9f9; border-radius: 10px;">{generated_story}</p>
|
166 |
+
</div>
|
167 |
+
""", unsafe_allow_html=True)
|
168 |
+
|
169 |
+
# Column 2: Generated Image
|
170 |
+
with col2:
|
171 |
+
st.subheader("Generated Image")
|
172 |
+
if isinstance(image, Image.Image):
|
173 |
+
st.image(image, caption="Generated from your prompt", use_column_width=True, output_format="JPEG")
|
174 |
+
else:
|
175 |
+
st.error(image)
|
176 |
+
|
177 |
+
# Sidebar Tips
|
178 |
+
st.sidebar.markdown("### Tips for Best Results:")
|
179 |
+
st.sidebar.markdown("""
|
180 |
+
- Keep **Max Tokens** between 50-150 for concise stories.
|
181 |
+
- Use **Temperature** around 0.7 for creative but coherent outputs.
|
182 |
+
- Longer text inputs generate more detailed stories.
|
183 |
+
""")
|
184 |
+
|
185 |
+
# Footer Section
|
186 |
+
st.markdown("""
|
187 |
+
<style>
|
188 |
+
.footer {
|
189 |
+
position: fixed;
|
190 |
+
bottom: 0;
|
191 |
+
width: 100%;
|
192 |
+
background-color: #2c3e50;
|
193 |
+
color: white;
|
194 |
+
text-align: center;
|
195 |
+
padding: 10px;
|
196 |
+
border-top: 2px solid #ff5e62;
|
197 |
+
}
|
198 |
+
</style>
|
199 |
+
<div class="footer">
|
200 |
+
Made with ❤️ by YASH | Contact: yashwantmanish@gmail.com
|
201 |
+
</div>
|
202 |
+
""", unsafe_allow_html=True)
|
203 |
+
|
204 |
+
# Add animation to buttons
|
205 |
+
st.markdown("""
|
206 |
+
<style>
|
207 |
+
.stButton > button:hover {
|
208 |
+
background-color: #ff5e62;
|
209 |
+
color: white;
|
210 |
+
transition: background-color 0.3s ease, color 0.3s ease;
|
211 |
+
}
|
212 |
+
</style>
|
213 |
+
""", unsafe_allow_html=True)
|