Spaces:
Sleeping
Sleeping
Santhosh54321
commited on
Commit
•
3c0eb32
1
Parent(s):
43ce299
Update app.py
Browse files
app.py
CHANGED
@@ -1,120 +1,168 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
import streamlit as st
|
4 |
-
|
5 |
-
|
6 |
-
from transformers import MBartForConditionalGeneration,
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
"Authorization": f"Bearer {
|
21 |
"Content-Type": "application/json"
|
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 |
return None
|
54 |
|
55 |
-
# Function to
|
56 |
-
def
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
return None
|
68 |
|
69 |
-
#
|
70 |
-
def
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
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("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|