Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,98 +1,127 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import whisper
|
3 |
import gradio as gr
|
4 |
from groq import Groq
|
5 |
from deep_translator import GoogleTranslator
|
6 |
-
from diffusers import StableDiffusionPipeline
|
7 |
-
import os
|
8 |
import torch
|
9 |
-
import openai
|
10 |
|
11 |
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
14 |
client = Groq(api_key=api_key)
|
15 |
|
16 |
-
model_id1 = "dreamlike-art/dreamlike-diffusion-1.0"
|
17 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id1, torch_dtype=torch.float16, use_safetensors=True)
|
18 |
-
pipe = pipe.to("cuda")
|
19 |
|
|
|
|
|
|
|
20 |
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
{"role": "user", "content":prompt}
|
27 |
-
],
|
28 |
-
model="llama-3.2-90b-text-preview"
|
29 |
-
)
|
30 |
-
chatbot_response = chat_completion.choices[0].message.content
|
31 |
-
return chatbot_response
|
32 |
|
33 |
-
|
34 |
-
if audio_path is None:
|
35 |
-
return "Please upload an audio file.", None, None, None
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
with open(audio_path, "rb") as file:
|
40 |
-
transcription = client.audio.transcriptions.create(
|
41 |
-
file=(os.path.basename(audio_path), file.read()),
|
42 |
-
model="whisper-large-v3",
|
43 |
-
language="ta",
|
44 |
-
response_format="verbose_json",
|
45 |
-
)
|
46 |
-
tamil_text = transcription.text
|
47 |
-
except Exception as e:
|
48 |
-
return f"An error occurred during transcription: {str(e)}", None, None, None
|
49 |
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
# Step 2: Translate Tamil to English
|
52 |
try:
|
53 |
-
|
54 |
-
|
55 |
except Exception as e:
|
56 |
-
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
if creative_text_option == "Generate Creative Text":
|
61 |
-
creative_text = generate_creative_text(translation)
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
# Step 4: Generate image (if selected)
|
65 |
-
image = None
|
66 |
-
if image_option == "Generate Image":
|
67 |
try:
|
68 |
-
|
69 |
-
|
70 |
-
pipe = pipe.to("cuda")
|
71 |
-
image = pipe(translation).images[0]
|
72 |
except Exception as e:
|
73 |
-
return tamil_text,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
# Create Gradio interface
|
78 |
-
with gr.Blocks(theme=gr.themes.Base()) as iface:
|
79 |
-
gr.Markdown("# Audio Transcription, Translation, Image & Creative Text Generation")
|
80 |
with gr.Row():
|
81 |
-
with gr.Column():
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
90 |
image_output = gr.Image(label="Generated Image")
|
91 |
-
submit_button.click(
|
92 |
-
fn=process_audio,
|
93 |
-
inputs=[audio_input, image_option, creative_text_option],
|
94 |
-
outputs=[tamil_text_output, translation_output, creative_text_output, image_output]
|
95 |
-
)
|
96 |
|
97 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from deep_translator import GoogleTranslator
|
4 |
+
from PIL import Image
|
5 |
+
import requests
|
6 |
+
import io
|
7 |
+
import time
|
8 |
import whisper
|
9 |
import gradio as gr
|
10 |
from groq import Groq
|
11 |
from deep_translator import GoogleTranslator
|
|
|
|
|
12 |
import torch
|
|
|
13 |
|
14 |
|
15 |
+
# Replace with your actual Hugging Face API details
|
16 |
+
os.environ['hf_key']
|
17 |
+
key = os.getenv('hf_key')
|
18 |
+
API_URL = "https://api-inference.huggingface.co/models/Artples/LAI-ImageGeneration-vSDXL-2"
|
19 |
+
headers = {"Authorization": f"Bearer {key}"}
|
20 |
|
21 |
+
os.environ['grog_key']
|
22 |
+
api_key = os.getenv('grog_key')
|
23 |
client = Groq(api_key=api_key)
|
24 |
|
|
|
|
|
|
|
25 |
|
26 |
+
def query_image_generation(payload, max_retries=5):
|
27 |
+
for attempt in range(max_retries):
|
28 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
29 |
|
30 |
+
if response.status_code == 503:
|
31 |
+
print(f"Model is still loading, retrying... Attempt {attempt + 1}/{max_retries}")
|
32 |
+
estimated_time = min(response.json().get("estimated_time", 60), 60)
|
33 |
+
time.sleep(estimated_time)
|
34 |
+
continue
|
35 |
|
36 |
+
if response.status_code != 200:
|
37 |
+
print(f"Error: Received status code {response.status_code}")
|
38 |
+
print(f"Response: {response.text}")
|
39 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
return response.content
|
|
|
|
|
42 |
|
43 |
+
print(f"Failed to generate image after {max_retries} attempts.")
|
44 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
def generate_image(prompt):
|
47 |
+
image_bytes = query_image_generation({"inputs": prompt})
|
48 |
+
|
49 |
+
if image_bytes is None:
|
50 |
+
return None
|
51 |
|
|
|
52 |
try:
|
53 |
+
image = Image.open(io.BytesIO(image_bytes)) # Opening the image from bytes
|
54 |
+
return image
|
55 |
except Exception as e:
|
56 |
+
print(f"Error: {e}")
|
57 |
+
return None
|
58 |
|
59 |
+
def process_audio_or_text(input_text, audio_path, generate_image_flag):
|
60 |
+
tamil_text, translation, image = None, None, None
|
|
|
|
|
61 |
|
62 |
+
if audio_path: # Prefer audio input
|
63 |
+
try:
|
64 |
+
with open(audio_path, "rb") as file:
|
65 |
+
transcription = client.audio.transcriptions.create(
|
66 |
+
file=(os.path.basename(audio_path), file.read()),
|
67 |
+
model="whisper-large-v3",
|
68 |
+
language="ta",
|
69 |
+
response_format="verbose_json",
|
70 |
+
)
|
71 |
+
tamil_text = transcription.text
|
72 |
+
except Exception as e:
|
73 |
+
return f"An error occurred during transcription: {str(e)}", None, None
|
74 |
|
|
|
|
|
|
|
75 |
try:
|
76 |
+
translator = GoogleTranslator(source='ta', target='en')
|
77 |
+
translation = translator.translate(tamil_text)
|
|
|
|
|
78 |
except Exception as e:
|
79 |
+
return tamil_text, f"An error occurred during translation: {str(e)}", None
|
80 |
+
|
81 |
+
elif input_text: # No audio input, so use text input
|
82 |
+
translation = input_text
|
83 |
+
|
84 |
+
|
85 |
+
# Generate chatbot response
|
86 |
+
try:
|
87 |
+
chat_completion = client.chat.completions.create(
|
88 |
+
messages=[{"role": "user", "content": translation}],
|
89 |
+
model="llama-3.2-90b-text-preview"
|
90 |
+
)
|
91 |
+
chatbot_response = chat_completion.choices[0].message.content
|
92 |
+
except Exception as e:
|
93 |
+
return None, f"An error occurred during chatbot interaction: {str(e)}", None
|
94 |
|
95 |
+
if generate_image_flag: # Generate image if the checkbox is checked
|
96 |
+
image = generate_image(translation)
|
97 |
+
|
98 |
+
return tamil_text, chatbot_response, image # Return both chatbot response and image (if generated)
|
99 |
+
|
100 |
+
with gr.Blocks() as iface:
|
101 |
+
gr.Markdown("# AI Chatbot and Image Generation App")
|
102 |
|
|
|
|
|
|
|
103 |
with gr.Row():
|
104 |
+
with gr.Column(scale=1): # Left side (Inputs and Buttons)
|
105 |
+
user_input = gr.Textbox(label="Enter Tamil text", placeholder="Type your message here...")
|
106 |
+
audio_input = gr.Audio(type="filepath", label=" Or upload audio (for Image Generation)")
|
107 |
+
image_generation_checkbox = gr.Checkbox(label="Generate Image", value=False)
|
108 |
+
|
109 |
+
# Buttons
|
110 |
+
submit_btn = gr.Button("Submit")
|
111 |
+
clear_btn = gr.Button("Clear")
|
112 |
+
|
113 |
+
with gr.Column(scale=1): # Right side (Outputs)
|
114 |
+
text_output_1 = gr.Textbox(label="Tamil Transcription / Chatbot Response", interactive=False)
|
115 |
+
text_output_2 = gr.Textbox(label="English Translation", interactive=False)
|
116 |
image_output = gr.Image(label="Generated Image")
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
+
# Connect the buttons to the functions
|
119 |
+
submit_btn.click(fn=process_audio_or_text,
|
120 |
+
inputs=[user_input, audio_input, image_generation_checkbox],
|
121 |
+
outputs=[text_output_1, text_output_2, image_output])
|
122 |
+
|
123 |
+
clear_btn.click(lambda: ("", None, False, "", "", None),
|
124 |
+
inputs=[],
|
125 |
+
outputs=[user_input, audio_input, image_generation_checkbox, text_output_1, text_output_2, image_output])
|
126 |
+
|
127 |
iface.launch()
|