Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,198 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 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 | 
            +
             | 
| 108 | 
            +
            1. **Input Tamil Text**:
         | 
| 109 | 
            +
               - 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).
         | 
| 110 | 
            +
             | 
| 111 | 
            +
            2. **Generate Translations**:
         | 
| 112 | 
            +
               - 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.
         | 
| 113 | 
            +
             | 
| 114 | 
            +
            3. **Generate Educational Content**:
         | 
| 115 | 
            +
               - 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.
         | 
| 116 | 
            +
             | 
| 117 | 
            +
            4. **Generate Images**:
         | 
| 118 | 
            +
               - 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.
         | 
| 119 | 
            +
             | 
| 120 | 
            +
            ---
         | 
| 121 | 
            +
             | 
| 122 | 
            +
            ### Features:
         | 
| 123 | 
            +
             | 
| 124 | 
            +
            - **Multilingual Translation**:
         | 
| 125 | 
            +
               - 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:
         | 
| 126 | 
            +
             | 
| 127 | 
            +
                 - **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)**.
         | 
| 128 | 
            +
             | 
| 129 | 
            +
            - **Temperature Adjustment**:
         | 
| 130 | 
            +
               - 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.
         | 
| 131 | 
            +
             | 
| 132 | 
            +
            - **Token Limit**:
         | 
| 133 | 
            +
               - Set the **maximum number of tokens** for content generation. This allows you to control the length of the generated educational content.
         | 
| 134 | 
            +
             | 
| 135 | 
            +
            - **Auto-Generated Image Prompts**:
         | 
| 136 | 
            +
               - 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.
         | 
| 137 | 
            +
             | 
| 138 | 
            +
            ---
         | 
| 139 | 
            +
             | 
| 140 | 
            +
            Enjoy the multimodal experience with **FusionMind** and explore its powerful translation, content generation, and image generation features!
         | 
| 141 | 
            +
                """)
         | 
| 142 | 
            +
             | 
| 143 | 
            +
            # Main Streamlit app
         | 
| 144 | 
            +
            def main():
         | 
| 145 | 
            +
                # Sidebar Menu
         | 
| 146 | 
            +
                st.sidebar.title("FusionMind Options")
         | 
| 147 | 
            +
                page = st.sidebar.radio("Select a page:", ["Main App", "User Guide"])
         | 
| 148 | 
            +
             | 
| 149 | 
            +
                if page == "User Guide":
         | 
| 150 | 
            +
                    show_user_guide()
         | 
| 151 | 
            +
                    return
         | 
| 152 | 
            +
             | 
| 153 | 
            +
                st.title("🅰️ℹ️ FusionMind ➡️ Multimodal")
         | 
| 154 | 
            +
             | 
| 155 | 
            +
                # Sidebar for temperature, token adjustment, and model selection
         | 
| 156 | 
            +
                st.sidebar.header("Settings")
         | 
| 157 | 
            +
                temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
         | 
| 158 | 
            +
                max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 400, 200)
         | 
| 159 | 
            +
             | 
| 160 | 
            +
                # Content generation model selection
         | 
| 161 | 
            +
                content_model = st.sidebar.selectbox("Select Content Generation Model", list(content_models.keys()), index=0)
         | 
| 162 | 
            +
             | 
| 163 | 
            +
                # Image generation model selection
         | 
| 164 | 
            +
                image_model = st.sidebar.selectbox("Select Image Generation Model", list(image_generation_urls.keys()), index=0)
         | 
| 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_local(tamil_input)
         | 
| 179 | 
            +
                        if english_text:
         | 
| 180 | 
            +
                            st.success(english_text)
         | 
| 181 | 
            +
             | 
| 182 | 
            +
                            # Step 2: Generate Educational Content
         | 
| 183 | 
            +
                            st.write("### Generated 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 (optional)
         | 
| 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()
         |