Santhosh54321 commited on
Commit
3c0eb32
1 Parent(s): 43ce299

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -110
app.py CHANGED
@@ -1,120 +1,168 @@
1
- import os
2
- import requests
3
  import streamlit as st
4
- from PIL import Image
5
- from io import BytesIO
6
- from transformers import MBartForConditionalGeneration, MBart50Tokenizer
7
- import time
8
-
9
- # Fetch the API keys from Hugging Face Secrets
10
- HUGGINGFACE_TOKEN = os.getenv("Hugging_face_token")
11
- GROQ_API_KEY = os.getenv("Groq_api")
12
-
13
- # Hugging Face API endpoint
14
- HF_API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
15
- hf_headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
16
-
17
- # Groq API endpoint
18
- groq_url = "https://api.groq.com/openai/v1/chat/completions"
19
- groq_headers = {
20
- "Authorization": f"Bearer {GROQ_API_KEY}",
21
  "Content-Type": "application/json"
22
  }
23
 
24
- # Function to query Hugging Face model for image generation
25
- def query_huggingface(payload):
26
- try:
27
- response = requests.post(HF_API_URL, headers=hf_headers, json=payload)
28
- if response.status_code != 200:
29
- return None
30
- return response.content
31
- except Exception:
32
- return None
 
 
 
 
 
33
 
34
- # Function to generate text using Groq API
35
- def generate_response(prompt):
36
- try:
37
- payload = {
38
- "model": "mixtral-8x7b-32768",
39
- "messages": [
40
- {"role": "system", "content": "You are a helpful assistant."},
41
- {"role": "user", "content": prompt}
42
- ],
43
- "max_tokens": 100,
44
- "temperature": 0.7
45
- }
46
- response = requests.post(groq_url, json=payload, headers=groq_headers)
47
- if response.status_code == 200:
48
- result = response.json()
49
- return result['choices'][0]['message']['content']
50
- else:
51
- return None
52
- except Exception:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  return None
54
 
55
- # Function to translate Tamil to English using MBart model
56
- def translate_tamil_to_english(tamil_text):
57
- try:
58
- model_name = "facebook/mbart-large-50-many-to-one-mmt"
59
- model = MBartForConditionalGeneration.from_pretrained(model_name)
60
- tokenizer = MBart50Tokenizer.from_pretrained(model_name, src_lang="ta_IN")
61
-
62
- inputs = tokenizer(tamil_text, return_tensors="pt")
63
- translated = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
64
- translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
65
- return translated_text
66
- except Exception:
 
 
 
 
67
  return None
68
 
69
- # Main function to generate text and image
70
- def generate_image_and_text(user_input):
71
- with st.spinner("Generating results..."):
72
- time.sleep(2) # Simulate some processing time
73
-
74
- # Translate Tamil to English
75
- english_input = translate_tamil_to_english(user_input)
76
- if not english_input:
77
- st.warning("Sorry, the translation model is unavailable right now 😥😥😥. Please try again later.")
78
- else:
79
- st.markdown("### Translated English Text:")
80
- st.write(english_input)
81
-
82
- # Generate text description (100 tokens) using Groq API
83
- if english_input:
84
- full_text_description = generate_response(english_input)
85
- if not full_text_description:
86
- st.warning("Sorry, the text generation model is unavailable right now 😥😥😥. Please try again later.")
87
- else:
88
- st.markdown("### Generated Text Response:")
89
- st.write(full_text_description)
90
-
91
- # Create image prompt based on the full text description
92
- image_prompt = generate_response(f"Create a concise image prompt from the following text: {full_text_description}")
93
- if not image_prompt:
94
- st.warning("Sorry, the image prompt model is unavailable right now 😥😥😥. Please try again later.")
95
- else:
96
- # Request an image based on the generated image prompt
97
- image_data = query_huggingface({"inputs": image_prompt})
98
- if not image_data:
99
- st.warning("Sorry, the image generation model is unavailable right now 😥😥😥. Please try again later.")
100
- else:
101
- try:
102
- # Load and display the image
103
- image = Image.open(BytesIO(image_data))
104
- st.image(image, caption="Generated Image", use_column_width=True)
105
- except Exception as e:
106
- st.error(f"Failed to display image: {e}")
107
-
108
- # Streamlit interface
109
- st.title("Multi-Modal Generator (Tamil to English)")
110
- st.write("Enter a prompt in Tamil to generate both text and an image.")
111
-
112
- # Input field for Tamil text
113
- user_input = st.text_input("Enter Tamil text here:")
114
-
115
- # Generate results when button is clicked
116
- if st.button("Generate"):
117
- if user_input:
118
- generate_image_and_text(user_input)
119
  else:
120
- st.error("Please enter a Tamil text.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import requests
3
+ import os
4
+ from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
5
+
6
+ # API keys for other features (optional)
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 for external services (optional)
12
+ Image_generation = {"Authorization": f"Bearer {Image_Token}"}
13
+ Content_generation = {
14
+ "Authorization": f"Bearer {Content_Token}",
15
+ "Content-Type": "application/json"
16
+ }
17
+ Image_Prompt = {
18
+ "Authorization": f"Bearer {Image_prompt_token}",
19
  "Content-Type": "application/json"
20
  }
21
 
22
+ # Text-to-Image Model API URLs
23
+ image_generation_urls = {
24
+ "black-forest-labs/FLUX.1-schnell": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
25
+ "CompVis/stable-diffusion-v1-4": "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4",
26
+ "black-forest-labs/FLUX.1-dev": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
27
+ }
28
+
29
+ # Default content generation model
30
+ content_models = {
31
+ "llama-3.1-70b-versatile": "llama-3.1-70b-versatile",
32
+ "llama3-8b-8192": "llama3-8b-8192",
33
+ "gemma2-9b-it": "gemma2-9b-it",
34
+ "mixtral-8x7b-32768": "mixtral-8x7b-32768"
35
+ }
36
 
37
+ # Load the translation model and tokenizer locally
38
+ @st.cache_resource
39
+ def load_translation_model():
40
+ model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-one-mmt")
41
+ tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-one-mmt")
42
+ return model, tokenizer
43
+
44
+ # Function to perform translation locally
45
+ def translate_text_local(text):
46
+ model, tokenizer = load_translation_model()
47
+ inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
48
+ translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
49
+ translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
50
+ return translated_text
51
+
52
+ # Function to query Groq content generation model (optional)
53
+ def generate_content(english_text, max_tokens, temperature, model):
54
+ url = "https://api.groq.com/openai/v1/chat/completions"
55
+ payload = {
56
+ "model": model,
57
+ "messages": [
58
+ {"role": "system", "content": "You are a creative and insightful writer."},
59
+ {"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
60
+ ],
61
+ "max_tokens": max_tokens,
62
+ "temperature": temperature
63
+ }
64
+ response = requests.post(url, json=payload, headers=Content_generation)
65
+ if response.status_code == 200:
66
+ result = response.json()
67
+ return result['choices'][0]['message']['content']
68
+ else:
69
+ st.error(f"Content Generation Error: {response.status_code}")
70
  return None
71
 
72
+ # Function to generate image prompt (optional)
73
+ def generate_image_prompt(english_text):
74
+ payload = {
75
+ "model": "mixtral-8x7b-32768",
76
+ "messages": [
77
+ {"role": "system", "content": "You are a professional Text to image prompt generator."},
78
+ {"role": "user", "content": f"Create a text to image generation prompt about {english_text} within 30 tokens."}
79
+ ],
80
+ "max_tokens": 30
81
+ }
82
+ response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=payload, headers=Image_Prompt)
83
+ if response.status_code == 200:
84
+ result = response.json()
85
+ return result['choices'][0]['message']['content']
86
+ else:
87
+ st.error(f"Prompt Generation Error: {response.status_code}")
88
  return None
89
 
90
+ # Function to generate an image from the prompt (optional)
91
+ def generate_image(image_prompt, model_url):
92
+ data = {"inputs": image_prompt}
93
+ response = requests.post(model_url, headers=Image_generation, json=data)
94
+ if response.status_code == 200:
95
+ return response.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  else:
97
+ st.error(f"Image Generation Error {response.status_code}: {response.text}")
98
+ return None
99
+
100
+ # User Guide Section
101
+ def show_user_guide():
102
+ st.title("FusionMind User Guide")
103
+ st.write("""
104
+ ### Welcome to the FusionMind User Guide!
105
+
106
+ ### How to use this app:
107
+ 1. **Input Tamil Text**: Enter Tamil text to be translated and processed.
108
+ 2. **Generate Translations**: The app will automatically translate Tamil to English.
109
+ 3. **Generate Educational Content**: Use the translated English text to generate content.
110
+ 4. **Generate Images**: Optionally, generate an image related to the translated text.
111
+ """)
112
+
113
+ # Main Streamlit app
114
+ def main():
115
+ # Sidebar Menu
116
+ st.sidebar.title("FusionMind Options")
117
+ page = st.sidebar.radio("Select a page:", ["Main App", "User Guide"])
118
+
119
+ if page == "User Guide":
120
+ show_user_guide()
121
+ return
122
+
123
+ st.title("🅰️ℹ️ FusionMind ➡️ Multimodal")
124
+
125
+ # Sidebar for temperature, token adjustment, and model selection
126
+ st.sidebar.header("Settings")
127
+ temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
128
+ max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 400, 200)
129
+
130
+ # Content generation model selection
131
+ content_model = st.sidebar.selectbox("Select Content Generation Model", list(content_models.keys()), index=0)
132
+
133
+ # Image generation model selection
134
+ image_model = st.sidebar.selectbox("Select Image Generation Model", list(image_generation_urls.keys()), index=0)
135
+
136
+ # Suggested inputs
137
+ st.write("## Suggested Inputs")
138
+ suggestions = ["தரவு அறிவியல்", "உளவியல்", "ராக்கெட் எப்படி வேலை செய்கிறது"]
139
+ selected_suggestion = st.selectbox("Select a suggestion or enter your own:", [""] + suggestions)
140
+
141
+ # Input box for user
142
+ tamil_input = st.text_input("Enter Tamil text (or select a suggestion):", selected_suggestion)
143
+
144
+ if st.button("Generate"):
145
+ # Step 1: Translation (Tamil to English)
146
+ if tamil_input:
147
+ st.write("### Translated English Text:")
148
+ english_text = translate_text_local(tamil_input)
149
+ if english_text:
150
+ st.success(english_text)
151
+
152
+ # Step 2: Generate Educational Content
153
+ st.write("### Generated Content:")
154
+ with st.spinner('Generating content...'):
155
+ content_output = generate_content(english_text, max_tokens, temperature, content_models[content_model])
156
+ if content_output:
157
+ st.success(content_output)
158
+
159
+ # Step 3: Generate Image from the prompt (optional)
160
+ st.write("### Generated Image:")
161
+ with st.spinner('Generating image...'):
162
+ image_prompt = generate_image_prompt(english_text)
163
+ image_data = generate_image(image_prompt, image_generation_urls[image_model])
164
+ if image_data:
165
+ st.image(image_data, caption="Generated Image")
166
+
167
+ if __name__ == "__main__":
168
+ main()