24Sureshkumar commited on
Commit
e97aebb
·
verified ·
1 Parent(s): ea1fae0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -44
app.py CHANGED
@@ -1,47 +1,46 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
 
6
- # Hugging Face token from secrets
7
- HF_TOKEN = st.secrets["HF_TOKEN"]
8
-
9
- # Streamlit page settings
10
- st.set_page_config(page_title="Tamil to Image Generator", layout="centered")
11
-
12
- st.title("🧠 Tamil to Image Generator 🎨")
13
- st.markdown("Enter Tamil text Translate to English → Generate a creative story → Create an AI Image")
14
-
15
- # Input Tamil text
16
- tamil_input = st.text_area("Enter Tamil text", placeholder="உலகின் அழகான கடற்கரை பற்றி சொல்...")
17
-
18
- if st.button("Generate Image"):
19
- if not tamil_input.strip():
20
- st.warning("Please enter some Tamil text.")
21
- else:
22
- with st.spinner("🔁 Translating Tamil to English..."):
23
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ta-en")
24
- translated = translator(tamil_input, max_length=100)[0]['translation_text']
25
-
26
- st.success("✅ Translation done")
27
- st.markdown(f"**📝 Translated Text:** `{translated}`")
28
-
29
- with st.spinner("🧠 Generating creative English text..."):
30
- generator = pipeline("text-generation", model="gpt2")
31
- prompt_text = f"Describe this beautifully: {translated}"
32
- generated = generator(prompt_text, max_length=80, do_sample=True, top_k=50)[0]['generated_text']
33
-
34
- st.success("✅ Text generation done")
35
- st.markdown(f"**🎨 Creative Description:** `{generated}`")
36
-
37
- with st.spinner("🖼️ Generating AI Image... (may take 20–30 seconds)"):
38
- pipe = StableDiffusionPipeline.from_pretrained(
39
- "runwayml/stable-diffusion-v1-5",
40
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
41
- use_auth_token=HF_TOKEN
42
- ).to("cuda" if torch.cuda.is_available() else "cpu")
43
-
44
- image = pipe(prompt=generated).images[0]
45
-
46
- st.success("✅ Image generated!")
47
- st.image(image, caption="🖼️ AI Generated Image", use_column_width=True)
 
1
+ import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
 
6
+ # 1. Tamil to English Translator
7
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ta-en")
8
+
9
+ # 2. English Text Generator (you can use GPT2 or any causal model)
10
+ generator = pipeline("text-generation", model="gpt2")
11
+
12
+ # 3. Image Generator using Stable Diffusion
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ image_pipe = StableDiffusionPipeline.from_pretrained(
15
+ "CompVis/stable-diffusion-v1-4",
16
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
17
+ )
18
+ image_pipe = image_pipe.to(device)
19
+
20
+ # 👇 Combined function
21
+ def generate_image_from_tamil(tamil_input):
22
+ # Step 1: Translate Tamil English
23
+ translated = translator(tamil_input, max_length=100)[0]['translation_text']
24
+
25
+ # Step 2: Generate English sentence based on translated input
26
+ generated = generator(translated, max_length=50, num_return_sequences=1)[0]['generated_text']
27
+
28
+ # Step 3: Generate Image based on English text
29
+ image = image_pipe(generated).images[0]
30
+
31
+ return translated, generated, image
32
+
33
+ # 🎨 Gradio UI
34
+ iface = gr.Interface(
35
+ fn=generate_image_from_tamil,
36
+ inputs=gr.Textbox(lines=2, label="Enter Tamil Text"),
37
+ outputs=[
38
+ gr.Textbox(label="Translated English Text"),
39
+ gr.Textbox(label="Generated English Prompt"),
40
+ gr.Image(label="Generated Image")
41
+ ],
42
+ title="Tamil to Image Generator 🌅",
43
+ description="Translates Tamil → English, generates story → creates image using Stable Diffusion."
44
+ )
45
+
46
+ iface.launch()