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