File size: 6,865 Bytes
07930ee
 
 
 
 
1f8970b
 
 
 
07930ee
 
1f8970b
 
 
 
 
 
 
 
f37d2cd
 
 
 
 
 
 
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
 
 
 
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
 
 
 
 
1f8970b
f37d2cd
 
 
 
 
 
 
 
1f8970b
 
 
 
5e70bfa
1f8970b
5e70bfa
1f8970b
5e70bfa
1f8970b
5e70bfa
 
1f8970b
5e70bfa
1f8970b
 
5e70bfa
1f8970b
5e70bfa
 
1f8970b
5e70bfa
1f8970b
 
 
5e70bfa
1f8970b
5e70bfa
 
1f8970b
 
 
 
5e70bfa
1f8970b
 
 
 
5e70bfa
1f8970b
f37d2cd
 
 
5e70bfa
f37d2cd
 
 
 
 
 
 
 
 
cd1164b
 
 
f37d2cd
 
 
 
 
cd1164b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f37d2cd
07930ee
 
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
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
def translate_text(text):
    payload = {"inputs": text}
    response = requests.post(translation_url, headers=Translate, json=payload)
    if response.status_code == 200:
        result = response.json()
        translated_text = result[0]['generated_text']
        return translated_text
    else:
        st.error(f"Translation Error {response.status_code}: {response.text}")
        st.write('Please try again or provide an English input 😥')
        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

# Main Streamlit app
def main():
    # Custom CSS for background, borders, and other styling
    st.markdown(
        """
        <style>
        /* Custom Background Image */
        body {
            background-image: url('https://images.unsplash.com/photo-1519125323398-675f0ddb6308');
            background-size: cover;
            background-position: center;
        }

        /* Main content container styling */
        .reportview-container {
            background: rgba(255, 255, 255, 0.85); /* Transparent white background */
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1); /* Light shadow effect */
        }

        /* Border around the generated result */
        .result-container {
            border: 2px solid #4CAF50; /* Green border */
            padding: 20px;
            border-radius: 10px;
            margin-top: 20px;
            animation: fadeIn 2s ease; /* Animation effect */
        }

        /* Animation for fading in the result */
        @keyframes fadeIn {
            0% { opacity: 0; }
            100% { opacity: 1; }
        }

        </style>
        """, unsafe_allow_html=True
    )

    st.title("🅰️ℹ️ FusionMind ➡️ Multimodal Generator 🤖")
    
    # 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, 500, 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)

    # Initialize a variable to store the English text
    english_text = None

    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 translation fails, ask for English input
        if not english_text:
            english_text = st.text_input("Translation failed. Please enter English text instead:")

        # Ensure the English text is available
        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()