Update app.py
Browse files
app.py
CHANGED
@@ -1,154 +1,149 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
from
|
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 |
-
progress=gr.Progress(track_tqdm=True),
|
35 |
-
):
|
36 |
-
if randomize_seed:
|
37 |
-
seed = random.randint(0, MAX_SEED)
|
38 |
-
|
39 |
-
generator = torch.Generator().manual_seed(seed)
|
40 |
-
|
41 |
-
image = pipe(
|
42 |
-
prompt=prompt,
|
43 |
-
negative_prompt=negative_prompt,
|
44 |
-
guidance_scale=guidance_scale,
|
45 |
-
num_inference_steps=num_inference_steps,
|
46 |
-
width=width,
|
47 |
-
height=height,
|
48 |
-
generator=generator,
|
49 |
-
).images[0]
|
50 |
-
|
51 |
-
return image, seed
|
52 |
-
|
53 |
-
|
54 |
-
examples = [
|
55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
56 |
-
"An astronaut riding a green horse",
|
57 |
-
"A delicious ceviche cheesecake slice",
|
58 |
-
]
|
59 |
-
|
60 |
-
css = """
|
61 |
-
#col-container {
|
62 |
-
margin: 0 auto;
|
63 |
-
max-width: 640px;
|
64 |
-
}
|
65 |
-
"""
|
66 |
-
|
67 |
-
with gr.Blocks(css=css) as demo:
|
68 |
-
with gr.Column(elem_id="col-container"):
|
69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
70 |
-
|
71 |
-
with gr.Row():
|
72 |
-
prompt = gr.Text(
|
73 |
-
label="Prompt",
|
74 |
-
show_label=False,
|
75 |
-
max_lines=1,
|
76 |
-
placeholder="Enter your prompt",
|
77 |
-
container=False,
|
78 |
)
|
|
|
79 |
|
80 |
-
|
|
|
81 |
|
82 |
-
result = gr.Image(label="Result", show_label=False)
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
placeholder="Enter a negative prompt",
|
89 |
-
visible=False,
|
90 |
-
)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
with gr.Row():
|
103 |
-
width = gr.Slider(
|
104 |
-
label="Width",
|
105 |
-
minimum=256,
|
106 |
-
maximum=MAX_IMAGE_SIZE,
|
107 |
-
step=32,
|
108 |
-
value=1024, # Replace with defaults that work for your model
|
109 |
-
)
|
110 |
-
|
111 |
-
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
-
minimum=256,
|
114 |
-
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=32,
|
116 |
-
value=1024, # Replace with defaults that work for your model
|
117 |
-
)
|
118 |
-
|
119 |
-
with gr.Row():
|
120 |
-
guidance_scale = gr.Slider(
|
121 |
-
label="Guidance scale",
|
122 |
-
minimum=0.0,
|
123 |
-
maximum=10.0,
|
124 |
-
step=0.1,
|
125 |
-
value=0.0, # Replace with defaults that work for your model
|
126 |
-
)
|
127 |
-
|
128 |
-
num_inference_steps = gr.Slider(
|
129 |
-
label="Number of inference steps",
|
130 |
-
minimum=1,
|
131 |
-
maximum=50,
|
132 |
-
step=1,
|
133 |
-
value=2, # Replace with defaults that work for your model
|
134 |
-
)
|
135 |
-
|
136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
137 |
-
gr.on(
|
138 |
-
triggers=[run_button.click, prompt.submit],
|
139 |
-
fn=infer,
|
140 |
-
inputs=[
|
141 |
-
prompt,
|
142 |
-
negative_prompt,
|
143 |
-
seed,
|
144 |
-
randomize_seed,
|
145 |
-
width,
|
146 |
-
height,
|
147 |
-
guidance_scale,
|
148 |
-
num_inference_steps,
|
149 |
-
],
|
150 |
-
outputs=[result, seed],
|
151 |
-
)
|
152 |
-
|
153 |
-
if __name__ == "__main__":
|
154 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import io
|
5 |
+
import re
|
6 |
+
from PIL import Image
|
7 |
+
from groq import Groq
|
8 |
+
|
9 |
+
# Set Your API Keys
|
10 |
+
# Use environment variables securely
|
11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
12 |
+
HF_API_KEY = os.getenv("HF_TOKEN")
|
13 |
+
|
14 |
+
if not GROQ_API_KEY or not HF_API_KEY:
|
15 |
+
raise ValueError("GROQ_API_KEY and HF_TOKEN must be set in the environment variables.")
|
16 |
+
# Initialize Groq API client
|
17 |
+
client = Groq(api_key=GROQ_API_KEY)
|
18 |
+
|
19 |
+
# Use a Public Hugging Face Image Model
|
20 |
+
HF_IMAGE_MODEL = "stabilityai/stable-diffusion-2-1"
|
21 |
+
|
22 |
+
|
23 |
+
# Function 1: Tamil Audio to Tamil Text (Transcription)
|
24 |
+
def transcribe_audio(audio_path):
|
25 |
+
if not audio_path:
|
26 |
+
return "Error: Please upload an audio file."
|
27 |
+
|
28 |
+
try:
|
29 |
+
with open(audio_path, "rb") as file:
|
30 |
+
transcription = client.audio.transcriptions.create(
|
31 |
+
file=(os.path.basename(audio_path), file.read()),
|
32 |
+
model="whisper-large-v3",
|
33 |
+
language="ta", # Tamil
|
34 |
+
response_format="verbose_json",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
)
|
36 |
+
return transcription.text.strip()
|
37 |
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error in transcription: {str(e)}"
|
40 |
|
|
|
41 |
|
42 |
+
# Function 2: Tamil Text to English Translation
|
43 |
+
def translate_tamil_to_english(tamil_text):
|
44 |
+
if not tamil_text:
|
45 |
+
return "Error: Please enter Tamil text for translation."
|
|
|
|
|
|
|
46 |
|
47 |
+
prompt = f"Translate this Tamil text to English: {tamil_text}\nGive only the translated text as output."
|
48 |
+
|
49 |
+
try:
|
50 |
+
response = client.chat.completions.create(
|
51 |
+
model="llama3-8b-8192", # Groq-supported model
|
52 |
+
messages=[{"role": "user", "content": prompt}],
|
53 |
+
)
|
54 |
+
translated_text = response.choices[0].message.content.strip()
|
55 |
+
|
56 |
+
# Fix: Remove unwanted XML tags like <think></think>
|
57 |
+
translated_text = re.sub(r"</?think>", "", translated_text).strip()
|
58 |
+
return translated_text
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
return f"Error in translation: {str(e)}"
|
62 |
+
|
63 |
+
|
64 |
+
# Function 3: English Text to Image Generation (Hugging Face)
|
65 |
+
def generate_image(english_text):
|
66 |
+
if not english_text:
|
67 |
+
return "Error: Please enter a description for image generation."
|
68 |
+
|
69 |
+
try:
|
70 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
71 |
+
payload = {"inputs": english_text}
|
72 |
+
|
73 |
+
response = requests.post(f"https://api-inference.huggingface.co/models/{HF_IMAGE_MODEL}",
|
74 |
+
headers=headers, json=payload)
|
75 |
+
response.raise_for_status()
|
76 |
+
image_bytes = response.content
|
77 |
+
|
78 |
+
# Check if the response is a valid image
|
79 |
+
if not image_bytes:
|
80 |
+
return "Error: Received empty response from API."
|
81 |
+
|
82 |
+
return Image.open(io.BytesIO(image_bytes))
|
83 |
+
|
84 |
+
except Exception as e:
|
85 |
+
return f"Error in image generation: {str(e)}"
|
86 |
+
|
87 |
+
|
88 |
+
# Function 4: English Text to AI-Generated Text
|
89 |
+
|
90 |
+
def generate_text(english_text):
|
91 |
+
if not english_text:
|
92 |
+
return "Please enter a prompt."
|
93 |
+
|
94 |
+
try:
|
95 |
+
response = client.chat.completions.create(
|
96 |
+
model="llama3-8b-8192", # Ensure you're using a valid model
|
97 |
+
messages=[{"role": "user", "content": english_text}],
|
98 |
+
)
|
99 |
+
|
100 |
+
# Extract the response content
|
101 |
+
generated_text = response.choices[0].message.content.strip()
|
102 |
+
|
103 |
+
# Remove unwanted XML-like tags
|
104 |
+
cleaned_text = re.sub(r"</?think>", "", generated_text).strip()
|
105 |
+
|
106 |
+
return cleaned_text
|
107 |
+
|
108 |
+
except Exception as e:
|
109 |
+
return f"Error in text generation: {str(e)}"
|
110 |
+
|
111 |
+
|
112 |
+
# Combined Function to Process All Steps
|
113 |
+
def process_audio(audio_path):
|
114 |
+
# Step 1: Tamil Audio → Tamil Text
|
115 |
+
tamil_text = transcribe_audio(audio_path)
|
116 |
+
if "Error" in tamil_text:
|
117 |
+
return tamil_text, None, None, None
|
118 |
+
|
119 |
+
# Step 2: Tamil Text → English Text
|
120 |
+
english_text = translate_tamil_to_english(tamil_text)
|
121 |
+
if "Error" in english_text:
|
122 |
+
return tamil_text, english_text, None, None
|
123 |
+
|
124 |
+
# Step 3: English Text → Image
|
125 |
+
image = generate_image(english_text)
|
126 |
+
if isinstance(image, str) and "Error" in image:
|
127 |
+
return tamil_text, english_text, None, None
|
128 |
+
|
129 |
+
# Step 4: English Text → AI-Generated Text
|
130 |
+
generated_text = generate_text(english_text)
|
131 |
+
return tamil_text, english_text, image, generated_text
|
132 |
+
|
133 |
+
|
134 |
+
# Create Gradio Interface
|
135 |
+
iface = gr.Interface(
|
136 |
+
fn=process_audio,
|
137 |
+
inputs=gr.Audio(type="filepath", label="Upload Tamil Audio"),
|
138 |
+
outputs=[
|
139 |
+
gr.Textbox(label="Transcribed Tamil Text"),
|
140 |
+
gr.Textbox(label="Translated English Text"),
|
141 |
+
gr.Image(label="Generated Image"),
|
142 |
+
gr.Textbox(label="Generated Text from English Prompt"),
|
143 |
+
],
|
144 |
+
title="Tamil Audio to AI Processing Pipeline",
|
145 |
+
description="Upload a Tamil audio file and get transcription, translation, image generation, and further text generation.",
|
146 |
+
)
|
147 |
|
148 |
+
# Launch Gradio App
|
149 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|