Spaces:
Sleeping
Sleeping
| 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(): | |
| 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, 300, 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() | |