Santhosh54321 commited on
Commit
0c18306
·
verified ·
1 Parent(s): ff07a9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -88
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('HUGGINGFACE_TOKEN')
7
- Image_Token = os.getenv('HUGGINGFACE_TOKEN')
8
- Content_Token = os.getenv('GROQ_API_KEY')
9
- Image_prompt_token = os.getenv('GROQ_API_KEY')
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
- response = requests.post(translation_url, headers=Translate, json=payload)
33
- if response.status_code == 200:
 
 
 
34
  result = response.json()
35
  translated_text = result[0]['generated_text']
36
  return translated_text
37
- else:
38
- st.error(f"Translation Error {response.status_code}: {response.text}")
39
- st.write(f'Please try after sometime 😥😥😥')
40
- return None
 
 
 
 
 
 
 
 
 
 
 
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
- # Custom CSS for background, borders, and other styling
93
- st.markdown(
94
- """
95
- <style>
96
- body {
97
- background-image: url('https://wallpapercave.com/wp/wp4008910.jpg');
98
- background-size: cover;
99
- }
100
- .reportview-container {
101
- background: rgba(255, 255, 255, 0.85);
102
- padding: 2rem;
103
- border-radius: 10px;
104
- box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
105
- }
106
- .result-container {
107
- border: 2px solid #4CAF50;
108
- padding: 20px;
109
- border-radius: 10px;
110
- margin-top: 20px;
111
- animation: fadeIn 2s ease;
112
- }
113
- @keyframes fadeIn {
114
- 0% { opacity: 0; }
115
- 100% { opacity: 1; }
116
- }
117
- .stButton button {
118
- background-color: #4CAF50;
119
- color: white;
120
- border-radius: 10px;
121
- padding: 10px;
122
- }
123
- .stButton button:hover {
124
- background-color: #45a049;
125
- transform: scale(1.05);
126
- transition: 0.2s ease-in-out;
127
- }
128
- </style>
129
- """, unsafe_allow_html=True
130
- )
131
-
132
- st.title("🅰️ℹ️ FusionMind ➡️ Multimodal Generator 🤖")
133
-
134
- # Sidebar for temperature and token adjustment
135
- st.sidebar.header("Settings")
136
- temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
137
- max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 400, 200)
138
-
139
- # Suggested inputs
140
- st.write("## Suggested Inputs")
141
- suggestions = ["தரவு அறிவியல்", "புதிய திறன்களைக் கற்றுக்கொள்வது எப்படி", "ராக்கெட் எப்படி வேலை செய்கிறது"]
142
- selected_suggestion = st.selectbox("Select a suggestion or enter your own:", [""] + suggestions)
143
-
144
- # Input box for user
145
- tamil_input = st.text_input("Enter Tamil text (or select a suggestion):", selected_suggestion)
146
-
147
- if st.button("Generate"):
148
- # Step 1: Translation (Tamil to English)
149
- if tamil_input:
150
- st.write("### Translated English Text:")
151
- english_text = translate_text(tamil_input)
152
- if english_text:
153
- st.success(english_text)
154
-
155
- # Step 2: Generate Educational Content
156
- st.write("### Generated Educational Content:")
157
- with st.spinner('Generating content...'):
158
- content_output = generate_content(english_text, max_tokens, temperature)
159
- if content_output:
160
- st.success(content_output)
161
-
162
- # Step 3: Generate Image from the prompt
163
- st.write("### Generated Image:")
164
- with st.spinner('Generating image...'):
165
- image_prompt = generate_image_prompt(english_text)
166
- image_data = generate_image(image_prompt)
167
- if image_data:
168
- st.image(image_data, caption="Generated Image")
 
 
 
 
 
 
 
 
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()