prasanth345 commited on
Commit
bb9f3a0
1 Parent(s): 4cda4fc

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +200 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import os
4
+ from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
5
+
6
+
7
+
8
+ # API keys for other features (optional)
9
+ Image_Token = os.getenv('Image_generation')
10
+ Content_Token = os.getenv('ContentGeneration')
11
+ Image_prompt_token = os.getenv('Prompt_generation')
12
+
13
+ # API Headers for external services (optional)
14
+ Image_generation = {"Authorization": f"Bearer {Image_Token}"}
15
+ Content_generation = {
16
+ "Authorization": f"Bearer {Content_Token}",
17
+ "Content-Type": "application/json"
18
+ }
19
+ Image_Prompt = {
20
+ "Authorization": f"Bearer {Image_prompt_token}",
21
+ "Content-Type": "application/json"
22
+ }
23
+
24
+ # Text-to-Image Model API URLs
25
+ image_generation_urls = {
26
+ "black-forest-labs/FLUX.1-schnell": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
27
+ "CompVis/stable-diffusion-v1-4": "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4",
28
+ "black-forest-labs/FLUX.1-dev": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
29
+ }
30
+
31
+ # Default content generation model
32
+ content_models = {
33
+ "llama-3.1-70b-versatile": "llama-3.1-70b-versatile",
34
+ "llama3-8b-8192": "llama3-8b-8192",
35
+ "gemma2-9b-it": "gemma2-9b-it",
36
+ "mixtral-8x7b-32768": "mixtral-8x7b-32768"
37
+ }
38
+
39
+ # Load the translation model and tokenizer locally
40
+ @st.cache_resource
41
+ def load_translation_model():
42
+ model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-one-mmt")
43
+ tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-one-mmt")
44
+ return model, tokenizer
45
+
46
+ # Function to perform translation locally
47
+ def translate_text_local(text):
48
+ model, tokenizer = load_translation_model()
49
+ inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
50
+ translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
51
+ translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
52
+ return translated_text
53
+
54
+ # Function to query Groq content generation model (optional)
55
+ def generate_content(english_text, max_tokens, temperature, model):
56
+ url = "https://api.groq.com/openai/v1/chat/completions"
57
+ payload = {
58
+ "model": model,
59
+ "messages": [
60
+ {"role": "system", "content": "You are a creative and insightful writer."},
61
+ {"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
62
+ ],
63
+ "max_tokens": max_tokens,
64
+ "temperature": temperature
65
+ }
66
+ response = requests.post(url, json=payload, headers=Content_generation)
67
+ if response.status_code == 200:
68
+ result = response.json()
69
+ return result['choices'][0]['message']['content']
70
+ else:
71
+ st.error(f"Content Generation Error: {response.status_code}")
72
+ return None
73
+
74
+ # Function to generate image prompt (optional)
75
+ def generate_image_prompt(english_text):
76
+ payload = {
77
+ "model": "mixtral-8x7b-32768",
78
+ "messages": [
79
+ {"role": "system", "content": "You are a professional Text to image prompt generator."},
80
+ {"role": "user", "content": f"Create a text to image generation prompt about {english_text} within 30 tokens."}
81
+ ],
82
+ "max_tokens": 30
83
+ }
84
+ response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=payload, headers=Image_Prompt)
85
+ if response.status_code == 200:
86
+ result = response.json()
87
+ return result['choices'][0]['message']['content']
88
+ else:
89
+ st.error(f"Prompt Generation Error: {response.status_code}")
90
+ return None
91
+
92
+ # Function to generate an image from the prompt (optional)
93
+ def generate_image(image_prompt, model_url):
94
+ data = {"inputs": image_prompt}
95
+ response = requests.post(model_url, headers=Image_generation, json=data)
96
+ if response.status_code == 200:
97
+ return response.content
98
+ else:
99
+ st.error(f"Image Generation Error {response.status_code}: {response.text}")
100
+ return None
101
+
102
+ # User Guide Section
103
+ def show_user_guide():
104
+ st.title("FusionMind User Guide")
105
+ st.write("""
106
+ ### Welcome to the FusionMind User Guide!
107
+
108
+ ### How to use this app:
109
+
110
+ 1. **Input Tamil Text**:
111
+ - 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).
112
+
113
+ 2. **Generate Translations**:
114
+ - 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.
115
+
116
+ 3. **Generate Educational Content**:
117
+ - 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.
118
+
119
+ 4. **Generate Images**:
120
+ - 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.
121
+
122
+ ---
123
+
124
+ ### Features:
125
+
126
+ - **Multilingual Translation**:
127
+ - 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:
128
+
129
+ - **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)**.
130
+
131
+ - **Temperature Adjustment**:
132
+ - 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.
133
+
134
+ - **Token Limit**:
135
+ - Set the **maximum number of tokens** for content generation. This allows you to control the length of the generated educational content.
136
+
137
+ - **Auto-Generated Image Prompts**:
138
+ - 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.
139
+
140
+ ---
141
+
142
+ Enjoy the multimodal experience with **FusionMind** and explore its powerful translation, content generation, and image generation features!
143
+ """)
144
+
145
+ # Main Streamlit app
146
+ def main():
147
+ # Sidebar Menu
148
+ st.sidebar.title("FusionMind Options")
149
+ page = st.sidebar.radio("Select a page:", ["Main App", "User Guide"])
150
+
151
+ if page == "User Guide":
152
+ show_user_guide()
153
+ return
154
+
155
+ st.title("🅰️ℹ️ FusionMind ➡️ Multimodal")
156
+
157
+ # Sidebar for temperature, token adjustment, and model selection
158
+ st.sidebar.header("Settings")
159
+ temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
160
+ max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 400, 200)
161
+
162
+ # Content generation model selection
163
+ content_model = st.sidebar.selectbox("Select Content Generation Model", list(content_models.keys()), index=0)
164
+
165
+ # Image generation model selection
166
+ image_model = st.sidebar.selectbox("Select Image Generation Model", list(image_generation_urls.keys()), index=0)
167
+
168
+ # Suggested inputs
169
+ st.write("## Suggested Inputs")
170
+ suggestions = ["தரவு அறிவியல்", "உளவியல்", "ராக்கெட் எப்படி வேலை செய்கிறது"]
171
+ selected_suggestion = st.selectbox("Select a suggestion or enter your own:", [""] + suggestions)
172
+
173
+ # Input box for user
174
+ tamil_input = st.text_input("Enter Tamil text (or select a suggestion):", selected_suggestion)
175
+
176
+ if st.button("Generate"):
177
+ # Step 1: Translation (Tamil to English)
178
+ if tamil_input:
179
+ st.write("### Translated English Text:")
180
+ english_text = translate_text_local(tamil_input)
181
+ if english_text:
182
+ st.success(english_text)
183
+
184
+ # Step 2: Generate Educational Content
185
+ st.write("### Generated Content:")
186
+ with st.spinner('Generating content...'):
187
+ content_output = generate_content(english_text, max_tokens, temperature, content_models[content_model])
188
+ if content_output:
189
+ st.success(content_output)
190
+
191
+ # Step 3: Generate Image from the prompt (optional)
192
+ st.write("### Generated Image:")
193
+ with st.spinner('Generating image...'):
194
+ image_prompt = generate_image_prompt(english_text)
195
+ image_data = generate_image(image_prompt, image_generation_urls[image_model])
196
+ if image_data:
197
+ st.image(image_data, caption="Generated Image")
198
+
199
+ if __name__ == "__main__":
200
+ main()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit==1.24.0
2
+ requests==2.31.0
3
+ transformers==4.33.3
4
+ torch==2.0.1
5
+ sentencepiece==0.1.99