File size: 8,509 Bytes
07930ee
 
 
 
 
2c53e0e
 
 
 
07930ee
 
1f8970b
 
 
 
 
 
 
 
f37d2cd
 
 
 
 
 
 
 
 
0c18306
f37d2cd
 
0c18306
 
 
 
 
f37d2cd
 
 
0c18306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f37d2cd
 
 
 
 
 
 
 
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
0c18306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f37d2cd
 
0c18306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f37d2cd
07930ee
ff07a9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
import streamlit as st
import requests
import os

# Fetch Hugging Face and Groq API keys from secrets
Transalate_token = os.getenv('HUGGINGFACE_TOKEN')
Image_Token = os.getenv('HUGGINGFACE_TOKEN')
Content_Token = os.getenv('GROQ_API_KEY')
Image_prompt_token = os.getenv('GROQ_API_KEY')

# API Headers
Translate = {"Authorization": f"Bearer {Transalate_token}"}
Image_generation = {"Authorization": f"Bearer {Image_Token}"}
Content_generation = {
    "Authorization": f"Bearer {Content_Token}",
    "Content-Type": "application/json"
}
Image_Prompt = {
    "Authorization": f"Bearer {Image_prompt_token}",
    "Content-Type": "application/json"
}

# Translation Model API URL (Tamil to English)
translation_url = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-one-mmt"

# Text-to-Image Model API URL
image_generation_url = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"

# Function to query Hugging Face translation model with try-except retry logic
def translate_text(text):
    payload = {"inputs": text}
    
    # Try block to handle the first attempt
    try:
        response = requests.post(translation_url, headers=Translate, json=payload)
        response.raise_for_status()  # Raise an error for bad status codes (non-200)
        result = response.json()
        translated_text = result[0]['generated_text']
        return translated_text

    except requests.exceptions.RequestException as e:
        st.warning(f"First attempt failed due to: {e}. Retrying...")

        # Retry the request once if it fails
        try:
            response = requests.post(translation_url, headers=Translate, json=payload)
            response.raise_for_status()  # Raise an error for bad status codes (non-200)
            result = response.json()
            translated_text = result[0]['generated_text']
            return translated_text

        except requests.exceptions.RequestException as e:
            st.error(f"Second attempt failed: {e}")
            return None

# Function to query Groq content generation model
def generate_content(english_text, max_tokens, temperature):
    url = "https://api.groq.com/openai/v1/chat/completions"
    payload = {
        "model": "llama-3.1-70b-versatile",
        "messages": [
            {"role": "system", "content": "You are a creative and insightful writer."},
            {"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
        ],
        "max_tokens": max_tokens,
        "temperature": temperature
    }
    response = requests.post(url, json=payload, headers=Content_generation)
    if response.status_code == 200:
        result = response.json()
        return result['choices'][0]['message']['content']
    else:
        st.error(f"Content Generation Error: {response.status_code}")
        return None

# Function to generate image prompt
def generate_image_prompt(english_text):
    payload = {
        "model": "mixtral-8x7b-32768",
        "messages": [
            {"role": "system", "content": "You are a professional Text to image prompt generator."},
            {"role": "user", "content": f"Create a text to image generation prompt about {english_text} within 30 tokens."}
        ],
        "max_tokens": 30
    }
    response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=payload, headers=Image_Prompt)
    if response.status_code == 200:
        result = response.json()
        return result['choices'][0]['message']['content']
    else:
        st.error(f"Prompt Generation Error: {response.status_code}")
        return None

# Function to generate an image from the prompt
def generate_image(image_prompt):
    data = {"inputs": image_prompt}
    response = requests.post(image_generation_url, headers=Image_generation, json=data)
    if response.status_code == 200:
        return response.content
    else:
        st.error(f"Image Generation Error {response.status_code}: {response.text}")
        return None

# User Guide content
def user_guide():
    st.title("User Guide")
    st.write("""
    ### How to use this app:

    1. **Input Tamil Text**: You can either select one of the suggested Tamil phrases or input your own.
    2. **Generate Translations**: The app will automatically translate Tamil input to English.
    3. **Generate Educational Content**: Based on the translated text, the app will generate educational content.
    4. **Generate Images**: The app will also generate an image based on the content.

    ### Features:
    - **Temperature Adjustment**: You can adjust the temperature for content creativity.
    - **Token Limit**: Set the maximum number of tokens for content generation.
    - **Retries**: If the translation fails, the app will retry automatically.
    
    Enjoy the multimodal experience with FusionMind!
    """)

# Main Streamlit app
def main():
    # Sidebar for navigation
    st.sidebar.title("Navigation")
    page = st.sidebar.radio("Go to", ["Home", "User Guide"])

    # If user selects "User Guide" page
    if page == "User Guide":
        user_guide()
    else:
        # Custom CSS for background, borders, and other styling
        st.markdown(
            """
            <style>
            body {
                background-image: url('https://wallpapercave.com/wp/wp4008910.jpg');
                background-size: cover;
            }
            .reportview-container {
                background: rgba(255, 255, 255, 0.85);
                padding: 2rem;
                border-radius: 10px;
                box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
            }
            .result-container {
                border: 2px solid #4CAF50;
                padding: 20px;
                border-radius: 10px;
                margin-top: 20px;
                animation: fadeIn 2s ease;
            }
            @keyframes fadeIn {
                0% { opacity: 0; }
                100% { opacity: 1; }
            }
            .stButton button {
                background-color: #4CAF50;
                color: white;
                border-radius: 10px;
                padding: 10px;
            }
            .stButton button:hover {
                background-color: #45a049;
                transform: scale(1.05);
                transition: 0.2s ease-in-out;
            }
            </style>
            """, unsafe_allow_html=True
        )

        st.title("🅰️ℹ️ FusionMind ➡️ Multimodal")

        # Sidebar for temperature and token adjustment
        st.sidebar.header("Settings")
        temperature = st.sidebar.slider("Select Temperature", 0.1, 1.0, 0.7)
        max_tokens = st.sidebar.slider("Max Tokens for Content Generation", 100, 400, 200)

        # Suggested inputs
        st.write("## Suggested Inputs")
        suggestions = ["தரவு அறிவியல்", "புதிய திறன்களைக் கற்றுக்கொள்வது எப்படி", "ராக்கெட் எப்படி வேலை செய்கிறது"]
        selected_suggestion = st.selectbox("Select a suggestion or enter your own:", [""] + suggestions)

        # Input box for user
        tamil_input = st.text_input("Enter Tamil text (or select a suggestion):", selected_suggestion)

        if st.button("Generate"):
            # Step 1: Translation (Tamil to English)
            if tamil_input:
                st.write("### Translated English Text:")
                english_text = translate_text(tamil_input)
                if english_text:
                    st.success(english_text)

                    # Step 2: Generate Educational Content
                    st.write("### Generated Educational Content:")
                    with st.spinner('Generating content...'):
                        content_output = generate_content(english_text, max_tokens, temperature)
                        if content_output:
                            st.success(content_output)

                    # Step 3: Generate Image from the prompt
                    st.write("### Generated Image:")
                    with st.spinner('Generating image...'):
                        image_prompt = generate_image_prompt(english_text)
                        image_data = generate_image(image_prompt)
                        if image_data:
                            st.image(image_data, caption="Generated Image")

if __name__ == "__main__":
    main()