Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
|
7 |
+
import time
|
8 |
+
|
9 |
+
# Fetch the API keys from Hugging Face Secrets
|
10 |
+
HUGGINGFACE_TOKEN = os.getenv("Hugging_face_token")
|
11 |
+
GROQ_API_KEY = os.getenv("Groq_api")
|
12 |
+
|
13 |
+
# Hugging Face API endpoint
|
14 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
15 |
+
hf_headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
|
16 |
+
|
17 |
+
# Groq API endpoint
|
18 |
+
groq_url = "https://api.groq.com/openai/v1/chat/completions"
|
19 |
+
groq_headers = {
|
20 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
21 |
+
"Content-Type": "application/json"
|
22 |
+
}
|
23 |
+
|
24 |
+
# Function to query Hugging Face model for image generation
|
25 |
+
def query_huggingface(payload):
|
26 |
+
response = requests.post(HF_API_URL, headers=hf_headers, json=payload)
|
27 |
+
if response.status_code != 200:
|
28 |
+
st.error(f"Error: {response.status_code} - {response.text}")
|
29 |
+
return None
|
30 |
+
return response.content
|
31 |
+
|
32 |
+
# Function to generate text using Groq API
|
33 |
+
def generate_response(prompt):
|
34 |
+
payload = {
|
35 |
+
"model": "mixtral-8x7b-32768",
|
36 |
+
"messages": [
|
37 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
38 |
+
{"role": "user", "content": prompt}
|
39 |
+
],
|
40 |
+
"max_tokens": 100,
|
41 |
+
"temperature": 0.7
|
42 |
+
}
|
43 |
+
response = requests.post(groq_url, json=payload, headers=groq_headers)
|
44 |
+
if response.status_code == 200:
|
45 |
+
result = response.json()
|
46 |
+
return result['choices'][0]['message']['content']
|
47 |
+
else:
|
48 |
+
st.error(f"Error: {response.status_code} - {response.text}")
|
49 |
+
return None
|
50 |
+
|
51 |
+
# Function to translate Tamil to English using MBart model
|
52 |
+
def translate_tamil_to_english(tamil_text):
|
53 |
+
model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
54 |
+
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
55 |
+
tokenizer = MBart50Tokenizer.from_pretrained(model_name, src_lang="ta_IN")
|
56 |
+
|
57 |
+
inputs = tokenizer(tamil_text, return_tensors="pt")
|
58 |
+
translated = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
|
59 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
60 |
+
return translated_text
|
61 |
+
|
62 |
+
# Main function to generate text and image
|
63 |
+
def generate_image_and_text(user_input):
|
64 |
+
with st.spinner("Generating results..."):
|
65 |
+
time.sleep(2) # Simulate some processing time
|
66 |
+
|
67 |
+
# Translate Tamil to English
|
68 |
+
english_input = translate_tamil_to_english(user_input)
|
69 |
+
if not english_input:
|
70 |
+
st.error("Error in translation.")
|
71 |
+
return
|
72 |
+
|
73 |
+
# Generate text description (100 tokens) and image prompt (30 tokens) using Groq API
|
74 |
+
full_text_description = generate_response(english_input)
|
75 |
+
if not full_text_description:
|
76 |
+
st.error("Error in text generation.")
|
77 |
+
return
|
78 |
+
|
79 |
+
# Create image prompt based on the full text description
|
80 |
+
image_prompt = generate_response(f"Create a concise image prompt from the following text: {full_text_description}")
|
81 |
+
if not image_prompt:
|
82 |
+
st.error("Error in generating image prompt.")
|
83 |
+
return
|
84 |
+
|
85 |
+
# Request an image based on the generated image prompt
|
86 |
+
image_data = query_huggingface({"inputs": image_prompt})
|
87 |
+
if not image_data:
|
88 |
+
st.error("Error in image generation.")
|
89 |
+
return
|
90 |
+
|
91 |
+
# Display the results
|
92 |
+
st.markdown("### Translated English Text:")
|
93 |
+
st.write(english_input)
|
94 |
+
|
95 |
+
st.markdown("### Generated Text Response:")
|
96 |
+
st.write(full_text_description)
|
97 |
+
|
98 |
+
try:
|
99 |
+
# Load and display the image
|
100 |
+
image = Image.open(BytesIO(image_data))
|
101 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
102 |
+
except Exception as e:
|
103 |
+
st.error(f"Failed to display image: {e}")
|
104 |
+
|
105 |
+
# Streamlit interface
|
106 |
+
st.title("Multi-Modal Generator (Tamil to English)")
|
107 |
+
st.write("Enter a prompt in Tamil to generate both text and an image.")
|
108 |
+
|
109 |
+
# Input field for Tamil text
|
110 |
+
user_input = st.text_input("Enter Tamil text here:")
|
111 |
+
|
112 |
+
# Generate results when button is clicked
|
113 |
+
if st.button("Generate"):
|
114 |
+
if user_input:
|
115 |
+
generate_image_and_text(user_input)
|
116 |
+
else:
|
117 |
+
st.error("Please enter a Tamil text.")
|