Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,10 @@ import requests
|
|
3 |
import os
|
4 |
|
5 |
# Fetch Hugging Face and Groq API keys from secrets
|
6 |
-
Transalate_token = os.getenv('
|
7 |
-
Image_Token = os.getenv('
|
8 |
-
Content_Token = os.getenv('
|
9 |
-
Image_prompt_token = os.getenv('
|
10 |
|
11 |
# API Headers
|
12 |
Translate = {"Authorization": f"Bearer {Transalate_token}"}
|
@@ -26,18 +26,32 @@ translation_url = "https://api-inference.huggingface.co/models/facebook/mbart-la
|
|
26 |
# Text-to-Image Model API URL
|
27 |
image_generation_url = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
28 |
|
29 |
-
# Function to query Hugging Face translation model
|
30 |
def translate_text(text):
|
31 |
payload = {"inputs": text}
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
result = response.json()
|
35 |
translated_text = result[0]['generated_text']
|
36 |
return translated_text
|
37 |
-
|
38 |
-
|
39 |
-
st.
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Function to query Groq content generation model
|
43 |
def generate_content(english_text, max_tokens, temperature):
|
@@ -87,85 +101,112 @@ def generate_image(image_prompt):
|
|
87 |
st.error(f"Image Generation Error {response.status_code}: {response.text}")
|
88 |
return None
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
# Main Streamlit app
|
91 |
def main():
|
92 |
-
#
|
93 |
-
st.
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
if __name__ == "__main__":
|
171 |
main()
|
|
|
3 |
import os
|
4 |
|
5 |
# Fetch Hugging Face and Groq API keys from secrets
|
6 |
+
Transalate_token = os.getenv('Translate')
|
7 |
+
Image_Token = os.getenv('Image_generation')
|
8 |
+
Content_Token = os.getenv('ContentGeneration')
|
9 |
+
Image_prompt_token = os.getenv('Prompt_generation')
|
10 |
|
11 |
# API Headers
|
12 |
Translate = {"Authorization": f"Bearer {Transalate_token}"}
|
|
|
26 |
# Text-to-Image Model API URL
|
27 |
image_generation_url = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
28 |
|
29 |
+
# Function to query Hugging Face translation model with try-except retry logic
|
30 |
def translate_text(text):
|
31 |
payload = {"inputs": text}
|
32 |
+
|
33 |
+
# Try block to handle the first attempt
|
34 |
+
try:
|
35 |
+
response = requests.post(translation_url, headers=Translate, json=payload)
|
36 |
+
response.raise_for_status() # Raise an error for bad status codes (non-200)
|
37 |
result = response.json()
|
38 |
translated_text = result[0]['generated_text']
|
39 |
return translated_text
|
40 |
+
|
41 |
+
except requests.exceptions.RequestException as e:
|
42 |
+
st.warning(f"First attempt failed due to: {e}. Retrying...")
|
43 |
+
|
44 |
+
# Retry the request once if it fails
|
45 |
+
try:
|
46 |
+
response = requests.post(translation_url, headers=Translate, json=payload)
|
47 |
+
response.raise_for_status() # Raise an error for bad status codes (non-200)
|
48 |
+
result = response.json()
|
49 |
+
translated_text = result[0]['generated_text']
|
50 |
+
return translated_text
|
51 |
+
|
52 |
+
except requests.exceptions.RequestException as e:
|
53 |
+
st.error(f"Second attempt failed: {e}")
|
54 |
+
return None
|
55 |
|
56 |
# Function to query Groq content generation model
|
57 |
def generate_content(english_text, max_tokens, temperature):
|
|
|
101 |
st.error(f"Image Generation Error {response.status_code}: {response.text}")
|
102 |
return None
|
103 |
|
104 |
+
# User Guide content
|
105 |
+
def user_guide():
|
106 |
+
st.title("User Guide")
|
107 |
+
st.write("""
|
108 |
+
### How to use this app:
|
109 |
+
|
110 |
+
1. **Input Tamil Text**: You can either select one of the suggested Tamil phrases or input your own.
|
111 |
+
2. **Generate Translations**: The app will automatically translate Tamil input to English.
|
112 |
+
3. **Generate Educational Content**: Based on the translated text, the app will generate educational content.
|
113 |
+
4. **Generate Images**: The app will also generate an image based on the content.
|
114 |
+
|
115 |
+
### Features:
|
116 |
+
- **Temperature Adjustment**: You can adjust the temperature for content creativity.
|
117 |
+
- **Token Limit**: Set the maximum number of tokens for content generation.
|
118 |
+
- **Retries**: If the translation fails, the app will retry automatically.
|
119 |
+
|
120 |
+
Enjoy the multimodal experience with FusionMind!
|
121 |
+
""")
|
122 |
+
|
123 |
# Main Streamlit app
|
124 |
def main():
|
125 |
+
# Sidebar for navigation
|
126 |
+
st.sidebar.title("Navigation")
|
127 |
+
page = st.sidebar.radio("Go to", ["Home", "User Guide"])
|
128 |
+
|
129 |
+
# If user selects "User Guide" page
|
130 |
+
if page == "User Guide":
|
131 |
+
user_guide()
|
132 |
+
else:
|
133 |
+
# Custom CSS for background, borders, and other styling
|
134 |
+
st.markdown(
|
135 |
+
"""
|
136 |
+
<style>
|
137 |
+
body {
|
138 |
+
background-image: url('https://wallpapercave.com/wp/wp4008910.jpg');
|
139 |
+
background-size: cover;
|
140 |
+
}
|
141 |
+
.reportview-container {
|
142 |
+
background: rgba(255, 255, 255, 0.85);
|
143 |
+
padding: 2rem;
|
144 |
+
border-radius: 10px;
|
145 |
+
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
|
146 |
+
}
|
147 |
+
.result-container {
|
148 |
+
border: 2px solid #4CAF50;
|
149 |
+
padding: 20px;
|
150 |
+
border-radius: 10px;
|
151 |
+
margin-top: 20px;
|
152 |
+
animation: fadeIn 2s ease;
|
153 |
+
}
|
154 |
+
@keyframes fadeIn {
|
155 |
+
0% { opacity: 0; }
|
156 |
+
100% { opacity: 1; }
|
157 |
+
}
|
158 |
+
.stButton button {
|
159 |
+
background-color: #4CAF50;
|
160 |
+
color: white;
|
161 |
+
border-radius: 10px;
|
162 |
+
padding: 10px;
|
163 |
+
}
|
164 |
+
.stButton button:hover {
|
165 |
+
background-color: #45a049;
|
166 |
+
transform: scale(1.05);
|
167 |
+
transition: 0.2s ease-in-out;
|
168 |
+
}
|
169 |
+
</style>
|
170 |
+
""", unsafe_allow_html=True
|
171 |
+
)
|
172 |
+
|
173 |
+
st.title("🅰️ℹ️ FusionMind ➡️ Multimodal")
|
174 |
+
|
175 |
+
# Sidebar for temperature and token adjustment
|
176 |
+
st.sidebar.header("Settings")
|
177 |
+
temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
|
178 |
+
max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 400, 200)
|
179 |
+
|
180 |
+
# Suggested inputs
|
181 |
+
st.write("## Suggested Inputs")
|
182 |
+
suggestions = ["தரவு அறிவியல்", "புதிய திறன்களைக் கற்றுக்கொள்வது எப்படி", "ராக்கெட் எப்படி வேலை செய்கிறது"]
|
183 |
+
selected_suggestion = st.selectbox("Select a suggestion or enter your own:", [""] + suggestions)
|
184 |
+
|
185 |
+
# Input box for user
|
186 |
+
tamil_input = st.text_input("Enter Tamil text (or select a suggestion):", selected_suggestion)
|
187 |
+
|
188 |
+
if st.button("Generate"):
|
189 |
+
# Step 1: Translation (Tamil to English)
|
190 |
+
if tamil_input:
|
191 |
+
st.write("### Translated English Text:")
|
192 |
+
english_text = translate_text(tamil_input)
|
193 |
+
if english_text:
|
194 |
+
st.success(english_text)
|
195 |
+
|
196 |
+
# Step 2: Generate Educational Content
|
197 |
+
st.write("### Generated Educational Content:")
|
198 |
+
with st.spinner('Generating content...'):
|
199 |
+
content_output = generate_content(english_text, max_tokens, temperature)
|
200 |
+
if content_output:
|
201 |
+
st.success(content_output)
|
202 |
+
|
203 |
+
# Step 3: Generate Image from the prompt
|
204 |
+
st.write("### Generated Image:")
|
205 |
+
with st.spinner('Generating image...'):
|
206 |
+
image_prompt = generate_image_prompt(english_text)
|
207 |
+
image_data = generate_image(image_prompt)
|
208 |
+
if image_data:
|
209 |
+
st.image(image_data, caption="Generated Image")
|
210 |
|
211 |
if __name__ == "__main__":
|
212 |
main()
|