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}"}
|
@@ -23,41 +23,45 @@ Image_Prompt = {
|
|
23 |
# Translation Model API URL (Tamil to English)
|
24 |
translation_url = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-one-mmt"
|
25 |
|
26 |
-
# Text-to-Image Model API
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
|
|
|
|
|
|
30 |
def translate_text(text):
|
31 |
payload = {"inputs": text}
|
32 |
-
|
33 |
-
|
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 |
-
|
42 |
-
st.
|
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):
|
58 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
59 |
payload = {
|
60 |
-
"model":
|
61 |
"messages": [
|
62 |
{"role": "system", "content": "You are a creative and insightful writer."},
|
63 |
{"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
|
@@ -92,150 +96,103 @@ def generate_image_prompt(english_text):
|
|
92 |
return None
|
93 |
|
94 |
# Function to generate an image from the prompt
|
95 |
-
def generate_image(image_prompt):
|
96 |
data = {"inputs": image_prompt}
|
97 |
-
response = requests.post(
|
98 |
if response.status_code == 200:
|
99 |
return response.content
|
100 |
else:
|
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 |
-
### Welcome to FusionMind Multimodel ---> Your one stop solution for content creation.
|
109 |
-
|
110 |
-
|
111 |
-
**How to use this app:**
|
112 |
-
|
113 |
-
1. **Input Tamil Text**:
|
114 |
-
- You can either select one of the suggested Tamil phrases or input your own text. The app primarily focuses on Tamil inputs, but it supports a wide range of other languages as well (see the list below).
|
115 |
-
|
116 |
-
2. **Generate Translations**:
|
117 |
-
- Once you've input your text, the app will automatically translate it to English. The translation model is a **many-to-one model**, meaning it can take input from various languages and translate it into English.
|
118 |
-
|
119 |
-
3. **Generate Educational Content**:
|
120 |
-
- After translating the text into English, the app will generate **educational content** based on the translated input. You can adjust the creativity of the content generation using the temperature slider, and control the length of the output with the token limit setting.
|
121 |
-
|
122 |
-
4. **Generate Images**:
|
123 |
-
- In addition to generating content, the app can also generate an **image** related to the translated content. You don’t need to worry about creating complex image prompts—FusionMind includes an automatic **image prompt generator** that will convert your input into a well-defined image prompt, ensuring better image generation results.
|
124 |
-
|
125 |
-
---
|
126 |
-
|
127 |
-
### Features:
|
128 |
-
|
129 |
-
- **Multilingual Translation**:
|
130 |
-
- FusionMind supports a **many-to-one translation model**, so you can input text in a wide variety of languages, not just Tamil. Below are the supported languages:
|
131 |
-
|
132 |
-
- **Arabic (ar_AR)**, **Czech (cs_CZ)**, **German (de_DE)**, **English (en_XX)**, **Spanish (es_XX)**, **Estonian (et_EE)**, **Finnish (fi_FI)**, **French (fr_XX)**, **Gujarati (gu_IN)**, **Hindi (hi_IN)**, **Italian (it_IT)**, **Japanese (ja_XX)**, **Kazakh (kk_KZ)**, **Korean (ko_KR)**, **Lithuanian (lt_LT)**, **Latvian (lv_LV)**, **Burmese (my_MM)**, **Nepali (ne_NP)**, **Dutch (nl_XX)**, **Romanian (ro_RO)**, **Russian (ru_RU)**, **Sinhala (si_LK)**, **Turkish (tr_TR)**, **Vietnamese (vi_VN)**, **Chinese (zh_CN)**, **Afrikaans (af_ZA)**, **Azerbaijani (az_AZ)**, **Bengali (bn_IN)**, **Persian (fa_IR)**, **Hebrew (he_IL)**, **Croatian (hr_HR)**, **Indonesian (id_ID)**, **Georgian (ka_GE)**, **Khmer (km_KH)**, **Macedonian (mk_MK)**, **Malayalam (ml_IN)**, **Mongolian (mn_MN)**, **Marathi (mr_IN)**, **Polish (pl_PL)**, **Pashto (ps_AF)**, **Portuguese (pt_XX)**, **Swedish (sv_SE)**, **Swahili (sw_KE)**, **Tamil (ta_IN)**, **Telugu (te_IN)**, **Thai (th_TH)**, **Tagalog (tl_XX)**, **Ukrainian (uk_UA)**, **Urdu (ur_PK)**, **Xhosa (xh_ZA)**, **Galician (gl_ES)**, **Slovene (sl_SI)**.
|
133 |
-
|
134 |
-
- **Temperature Adjustment**:
|
135 |
-
- You can adjust the **temperature** of the content generation. A **higher temperature** makes the content more creative and varied, while a **lower temperature** generates more focused and deterministic responses.
|
136 |
-
|
137 |
-
- **Token Limit**:
|
138 |
-
- Set the **maximum number of tokens** for content generation. This allows you to control the length of the generated educational content.
|
139 |
-
|
140 |
-
- **Automatic Retries**:
|
141 |
-
- If a translation request fails due to any reason, the app is designed to **automatically retry**, ensuring a smooth experience.
|
142 |
-
|
143 |
-
- **Auto-Generated Image Prompts**:
|
144 |
-
- One of the unique features of FusionMind is the **auto-generated image prompts**. Even if you're not experienced in creating detailed prompts for image generation, the app will take care of this for you. It automatically converts the translated text or content into a well-defined prompt that produces more accurate and high-quality images.
|
145 |
-
|
146 |
-
---
|
147 |
-
|
148 |
-
Enjoy the multimodal experience with **FusionMind** and explore its powerful translation, content generation, and image generation features!
|
149 |
-
|
150 |
-
""")
|
151 |
-
|
152 |
# Main Streamlit app
|
153 |
def main():
|
154 |
-
#
|
155 |
-
st.
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
.
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
239 |
|
240 |
if __name__ == "__main__":
|
241 |
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}"}
|
|
|
23 |
# Translation Model API URL (Tamil to English)
|
24 |
translation_url = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-one-mmt"
|
25 |
|
26 |
+
# Text-to-Image Model API URLs
|
27 |
+
image_generation_urls = {
|
28 |
+
"black-forest-labs/FLUX.1-schnell": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
|
29 |
+
"CompVis/stable-diffusion-v1-4": "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4",
|
30 |
+
"black-forest-labs/FLUX.1-dev": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
31 |
+
}
|
32 |
+
|
33 |
+
# Default image generation model
|
34 |
+
default_image_model = "black-forest-labs/FLUX.1-schnell"
|
35 |
+
|
36 |
+
# Content generation models
|
37 |
+
content_models = {
|
38 |
+
"llama-3.1-70b-versatile": "llama-3.1-70b-versatile",
|
39 |
+
"llama3-8b-8192": "llama3-8b-8192",
|
40 |
+
"gemma2-9b-it": "gemma2-9b-it",
|
41 |
+
"mixtral-8x7b-32768": "mixtral-8x7b-32768"
|
42 |
+
}
|
43 |
|
44 |
+
# Default content generation model
|
45 |
+
default_content_model = "llama-3.1-70b-versatile"
|
46 |
+
|
47 |
+
# Function to query Hugging Face translation model
|
48 |
def translate_text(text):
|
49 |
payload = {"inputs": text}
|
50 |
+
response = requests.post(translation_url, headers=Translate, json=payload)
|
51 |
+
if response.status_code == 200:
|
|
|
|
|
|
|
52 |
result = response.json()
|
53 |
translated_text = result[0]['generated_text']
|
54 |
return translated_text
|
55 |
+
else:
|
56 |
+
st.error(f"Translation Error {response.status_code}: {response.text}")
|
57 |
+
st.write(f'Please try after sometime 😥😥😥')
|
58 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Function to query Groq content generation model
|
61 |
+
def generate_content(english_text, max_tokens, temperature, model):
|
62 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
63 |
payload = {
|
64 |
+
"model": model,
|
65 |
"messages": [
|
66 |
{"role": "system", "content": "You are a creative and insightful writer."},
|
67 |
{"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
|
|
|
96 |
return None
|
97 |
|
98 |
# Function to generate an image from the prompt
|
99 |
+
def generate_image(image_prompt, model_url):
|
100 |
data = {"inputs": image_prompt}
|
101 |
+
response = requests.post(model_url, headers=Image_generation, json=data)
|
102 |
if response.status_code == 200:
|
103 |
return response.content
|
104 |
else:
|
105 |
st.error(f"Image Generation Error {response.status_code}: {response.text}")
|
106 |
return None
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# Main Streamlit app
|
109 |
def main():
|
110 |
+
# Custom CSS for background, borders, and other styling
|
111 |
+
st.markdown(
|
112 |
+
"""
|
113 |
+
<style>
|
114 |
+
body {
|
115 |
+
background-image: url('https://wallpapercave.com/wp/wp4008910.jpg');
|
116 |
+
background-size: cover;
|
117 |
+
}
|
118 |
+
.reportview-container {
|
119 |
+
background: rgba(255, 255, 255, 0.85);
|
120 |
+
padding: 2rem;
|
121 |
+
border-radius: 10px;
|
122 |
+
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
|
123 |
+
}
|
124 |
+
.result-container {
|
125 |
+
border: 2px solid #4CAF50;
|
126 |
+
padding: 20px;
|
127 |
+
border-radius: 10px;
|
128 |
+
margin-top: 20px;
|
129 |
+
animation: fadeIn 2s ease;
|
130 |
+
}
|
131 |
+
@keyframes fadeIn {
|
132 |
+
0% { opacity: 0; }
|
133 |
+
100% { opacity: 1; }
|
134 |
+
}
|
135 |
+
.stButton button {
|
136 |
+
background-color: #4CAF50;
|
137 |
+
color: white;
|
138 |
+
border-radius: 10px;
|
139 |
+
padding: 10px;
|
140 |
+
}
|
141 |
+
.stButton button:hover {
|
142 |
+
background-color: #45a049;
|
143 |
+
transform: scale(1.05);
|
144 |
+
transition: 0.2s ease-in-out;
|
145 |
+
}
|
146 |
+
</style>
|
147 |
+
""", unsafe_allow_html=True
|
148 |
+
)
|
149 |
+
|
150 |
+
st.title("🅰️ℹ️ FusionMind ➡️ Multimodal")
|
151 |
+
|
152 |
+
# Sidebar for temperature, token adjustment, and model selection
|
153 |
+
st.sidebar.header("Settings")
|
154 |
+
temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
|
155 |
+
max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 400, 200)
|
156 |
+
|
157 |
+
# Content generation model selection
|
158 |
+
content_model = st.sidebar.selectbox("Select Content Generation Model", list(content_models.keys()), index=0)
|
159 |
+
|
160 |
+
# Image generation model selection
|
161 |
+
image_model = st.sidebar.selectbox("Select Image Generation Model", list(image_generation_urls.keys()), index=0)
|
162 |
+
|
163 |
+
# Reminder about model availability
|
164 |
+
st.sidebar.warning("Note: Based on availability, some models might not work. Please try another model if an error occurs.")
|
165 |
+
|
166 |
+
# Suggested inputs
|
167 |
+
st.write("## Suggested Inputs")
|
168 |
+
suggestions = ["தரவு அறிவியல்", "புதிய திறன்களைக் கற்றுக்கொள்வது எப்படி", "ராக்கெட் எப்படி வேலை செய்கிறது"]
|
169 |
+
selected_suggestion = st.selectbox("Select a suggestion or enter your own:", [""] + suggestions)
|
170 |
+
|
171 |
+
# Input box for user
|
172 |
+
tamil_input = st.text_input("Enter Tamil text (or select a suggestion):", selected_suggestion)
|
173 |
+
|
174 |
+
if st.button("Generate"):
|
175 |
+
# Step 1: Translation (Tamil to English)
|
176 |
+
if tamil_input:
|
177 |
+
st.write("### Translated English Text:")
|
178 |
+
english_text = translate_text(tamil_input)
|
179 |
+
if english_text:
|
180 |
+
st.success(english_text)
|
181 |
+
|
182 |
+
# Step 2: Generate Educational Content
|
183 |
+
st.write("### Generated Educational Content:")
|
184 |
+
with st.spinner('Generating content...'):
|
185 |
+
content_output = generate_content(english_text, max_tokens, temperature, content_models[content_model])
|
186 |
+
if content_output:
|
187 |
+
st.success(content_output)
|
188 |
+
|
189 |
+
# Step 3: Generate Image from the prompt
|
190 |
+
st.write("### Generated Image:")
|
191 |
+
with st.spinner('Generating image...'):
|
192 |
+
image_prompt = generate_image_prompt(english_text)
|
193 |
+
image_data = generate_image(image_prompt, image_generation_urls[image_model])
|
194 |
+
if image_data:
|
195 |
+
st.image(image_data, caption="Generated Image")
|
196 |
|
197 |
if __name__ == "__main__":
|
198 |
main()
|