Spaces:
Runtime error
Runtime error
raheemuddin
commited on
S3 upload output
Browse files
app.py
CHANGED
@@ -1,9 +1,13 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import boto3
|
3 |
import os
|
|
|
4 |
|
5 |
hf_token = os.environ.get("HF_TOKEN")
|
6 |
tasomi_model = os.environ.get("TASOMI_MODEL")
|
|
|
|
|
7 |
m=gr.load(tasomi_model, src="models",hf_token=hf_token)
|
8 |
|
9 |
def detect_language(text):
|
@@ -15,6 +19,15 @@ def detect_language(text):
|
|
15 |
detected_language = response['Languages'][0]['LanguageCode']
|
16 |
return detected_language
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def translate_and_predict(text):
|
19 |
"""Translates text and generates an image using Stable Diffusion."""
|
20 |
source_language_code=detect_language(text)
|
@@ -25,15 +38,19 @@ def translate_and_predict(text):
|
|
25 |
translation_result = translate.translate_text(Text=text, SourceLanguageCode=source_language_code, TargetLanguageCode=target_language_code)
|
26 |
translated_text = translation_result.get('TranslatedText')
|
27 |
print("translated_text:" , translated_text)
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Define the Gradio interface
|
32 |
iface = gr.Interface(
|
33 |
fn=translate_and_predict, # Since we're only displaying an image, there's no function to call
|
34 |
inputs=["text"], # Input: text, source language, target language
|
35 |
-
outputs="image", # Output: generated image
|
36 |
-
title="
|
37 |
)
|
38 |
|
39 |
# Launch the interface
|
|
|
1 |
+
import uuid
|
2 |
import gradio as gr
|
3 |
import boto3
|
4 |
import os
|
5 |
+
from PIL import Image
|
6 |
|
7 |
hf_token = os.environ.get("HF_TOKEN")
|
8 |
tasomi_model = os.environ.get("TASOMI_MODEL")
|
9 |
+
s3_bucket = os.environ.get("IMCAPSULE_EXTERNAL_BUCKET")
|
10 |
+
|
11 |
m=gr.load(tasomi_model, src="models",hf_token=hf_token)
|
12 |
|
13 |
def detect_language(text):
|
|
|
19 |
detected_language = response['Languages'][0]['LanguageCode']
|
20 |
return detected_language
|
21 |
|
22 |
+
def upload_file(file_path):
|
23 |
+
s3 = boto3.client('s3')
|
24 |
+
key='public/' + os.path.basename(file_path)
|
25 |
+
# Upload the file
|
26 |
+
s3.upload_file(file_path, s3_bucket, key)
|
27 |
+
s3_path = f"{s3.meta.endpoint_url}/{s3_bucket}/{key}"
|
28 |
+
print(f'File uploaded successfully. s3_path= {s3_path} ')
|
29 |
+
return s3_path
|
30 |
+
|
31 |
def translate_and_predict(text):
|
32 |
"""Translates text and generates an image using Stable Diffusion."""
|
33 |
source_language_code=detect_language(text)
|
|
|
38 |
translation_result = translate.translate_text(Text=text, SourceLanguageCode=source_language_code, TargetLanguageCode=target_language_code)
|
39 |
translated_text = translation_result.get('TranslatedText')
|
40 |
print("translated_text:" , translated_text)
|
41 |
+
gen_image=m(translated_text)
|
42 |
+
webp_image = Image.open(gen_image)
|
43 |
+
converted_image_path = f"./tasomi-{uuid.uuid4().hex}.jpg"
|
44 |
+
webp_image.save(converted_image_path, "JPEG")
|
45 |
+
s3_path = upload_file(converted_image_path)
|
46 |
+
return s3_path,s3_path
|
47 |
|
48 |
# Define the Gradio interface
|
49 |
iface = gr.Interface(
|
50 |
fn=translate_and_predict, # Since we're only displaying an image, there's no function to call
|
51 |
inputs=["text"], # Input: text, source language, target language
|
52 |
+
outputs=["image","text"], # Output: generated image
|
53 |
+
title="Tasomi Images",
|
54 |
)
|
55 |
|
56 |
# Launch the interface
|