Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,7 @@
|
|
| 1 |
-
import whisper
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from groq import Groq
|
| 4 |
-
from deep_translator import GoogleTranslator
|
| 5 |
-
from diffusers import StableDiffusionPipeline
|
| 6 |
-
import os
|
| 7 |
-
import torch
|
| 8 |
-
import openai
|
| 9 |
-
from huggingface_hub import InferenceApi
|
| 10 |
-
from PIL import Image
|
| 11 |
-
import requests
|
| 12 |
-
import io
|
| 13 |
-
import time
|
| 14 |
-
|
| 15 |
-
# Set up Groq API key
|
| 16 |
-
api_key = os.getenv("gkannada_key")
|
| 17 |
-
client = Groq(api_key=api_key)
|
| 18 |
-
|
| 19 |
-
# Hugging Face API details for image generation
|
| 20 |
-
key = os.getenv("h_key")
|
| 21 |
-
API_URL = "https://api-inference.huggingface.co/models/Artples/LAI-ImageGeneration-vSDXL-2"
|
| 22 |
-
headers = {"Authorization": f"Bearer {key}"}
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Function for querying image generation with retries
|
| 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 |
-
# Function for generating an image from text
|
| 47 |
-
def generate_image(prompt):
|
| 48 |
-
image_bytes = query_image_generation({"inputs": prompt})
|
| 49 |
-
|
| 50 |
-
if image_bytes is None:
|
| 51 |
-
return None
|
| 52 |
-
|
| 53 |
-
try:
|
| 54 |
-
image = Image.open(io.BytesIO(image_bytes)) # Opening the image from bytes
|
| 55 |
-
return image
|
| 56 |
-
except Exception as e:
|
| 57 |
-
print(f"Error: {e}")
|
| 58 |
-
return None
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# Updated function for text generation using the new API structure
|
| 62 |
-
def generate_creative_text(prompt):
|
| 63 |
-
chat_completion = client.chat.completions.create(
|
| 64 |
-
messages=[
|
| 65 |
-
{"role": "user", "content":prompt}
|
| 66 |
-
],
|
| 67 |
-
model="llama-3.2-90b-text-preview"
|
| 68 |
-
)
|
| 69 |
-
chatbot_response = chat_completion.choices[0].message.content
|
| 70 |
-
return chatbot_response
|
| 71 |
-
|
| 72 |
-
|
| 73 |
def process_audio(audio_path, image_option, creative_text_option):
|
| 74 |
if audio_path is None:
|
| 75 |
return "Please upload an audio file.", None, None, None
|
| 76 |
-
|
| 77 |
# Step 1: Transcribe audio
|
| 78 |
try:
|
| 79 |
with open(audio_path, "rb") as file:
|
|
@@ -83,50 +11,30 @@ def process_audio(audio_path, image_option, creative_text_option):
|
|
| 83 |
language="kn",
|
| 84 |
response_format="verbose_json",
|
| 85 |
)
|
| 86 |
-
|
| 87 |
except Exception as e:
|
| 88 |
return f"An error occurred during transcription: {str(e)}", None, None, None
|
| 89 |
-
|
|
|
|
| 90 |
try:
|
| 91 |
translator = GoogleTranslator(source='kn', target='en')
|
| 92 |
translation = translator.translate(kannada_text)
|
| 93 |
except Exception as e:
|
| 94 |
return kannada_text, f"An error occurred during translation: {str(e)}", None, None
|
| 95 |
-
|
| 96 |
# Step 3: Generate creative text (if selected)
|
| 97 |
creative_text = None
|
| 98 |
if creative_text_option == "Generate Creative Text":
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
# Step 4: Generate image (if selected)
|
| 102 |
image = None
|
| 103 |
if image_option == "Generate Image":
|
| 104 |
image = generate_image(translation)
|
| 105 |
if image is None:
|
| 106 |
-
return kannada_text, translation, creative_text,
|
| 107 |
-
|
| 108 |
return kannada_text, translation, creative_text, image
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# Create Gradio interface
|
| 112 |
-
with gr.Blocks(theme=gr.themes.Base()) as iface:
|
| 113 |
-
gr.Markdown("# Audio Transcription, Translation, Image & Creative Text Generation")
|
| 114 |
-
with gr.Row():
|
| 115 |
-
with gr.Column():
|
| 116 |
-
audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
| 117 |
-
image_option = gr.Dropdown(["Generate Image", "Skip Image"], label="Image Generation", value="Generate Image")
|
| 118 |
-
creative_text_option = gr.Dropdown(["Generate Creative Text", "Skip Creative Text"], label="Creative Text Generation", value="Generate Creative Text")
|
| 119 |
-
submit_button = gr.Button("Process Audio")
|
| 120 |
-
with gr.Column():
|
| 121 |
-
kannada_text_output = gr.Textbox(label="kannada Transcription")
|
| 122 |
-
translation_output = gr.Textbox(label="English Translation")
|
| 123 |
-
creative_text_output = gr.Textbox(label="Creative Text")
|
| 124 |
-
image_output = gr.Image(label="Generated Image")
|
| 125 |
-
submit_button.click(
|
| 126 |
-
fn=process_audio,
|
| 127 |
-
inputs=[audio_input, image_option, creative_text_option],
|
| 128 |
-
outputs=[kannada_text_output, translation_output, creative_text_output, image_output]
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
# Launch the interface
|
| 132 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def process_audio(audio_path, image_option, creative_text_option):
|
| 2 |
if audio_path is None:
|
| 3 |
return "Please upload an audio file.", None, None, None
|
| 4 |
+
|
| 5 |
# Step 1: Transcribe audio
|
| 6 |
try:
|
| 7 |
with open(audio_path, "rb") as file:
|
|
|
|
| 11 |
language="kn",
|
| 12 |
response_format="verbose_json",
|
| 13 |
)
|
| 14 |
+
kannada_text = transcription.text
|
| 15 |
except Exception as e:
|
| 16 |
return f"An error occurred during transcription: {str(e)}", None, None, None
|
| 17 |
+
|
| 18 |
+
# Step 2: Translate Kannada to English
|
| 19 |
try:
|
| 20 |
translator = GoogleTranslator(source='kn', target='en')
|
| 21 |
translation = translator.translate(kannada_text)
|
| 22 |
except Exception as e:
|
| 23 |
return kannada_text, f"An error occurred during translation: {str(e)}", None, None
|
| 24 |
+
|
| 25 |
# Step 3: Generate creative text (if selected)
|
| 26 |
creative_text = None
|
| 27 |
if creative_text_option == "Generate Creative Text":
|
| 28 |
+
try:
|
| 29 |
+
creative_text = generate_creative_text(translation)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
creative_text = f"An error occurred during creative text generation: {str(e)}"
|
| 32 |
+
|
| 33 |
# Step 4: Generate image (if selected)
|
| 34 |
image = None
|
| 35 |
if image_option == "Generate Image":
|
| 36 |
image = generate_image(translation)
|
| 37 |
if image is None:
|
| 38 |
+
return kannada_text, translation, creative_text, "An error occurred during image generation."
|
| 39 |
+
|
| 40 |
return kannada_text, translation, creative_text, image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|