Spaces:
Runtime error
Runtime error
Add Auto text Translation
Browse files
app.py
CHANGED
@@ -1,9 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
|
4 |
hf_token = os.environ.get("HF_TOKEN")
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import boto3
|
3 |
+
import os
|
4 |
|
5 |
hf_token = os.environ.get("HF_TOKEN")
|
6 |
+
m=gr.load("imcapsule/tasomi-capsule", src="models",hf_token=hf_token)
|
7 |
|
8 |
+
def detect_language(text):
|
9 |
+
# Initialize the Comprehend client
|
10 |
+
comprehend = boto3.client('comprehend', region_name='ap-northeast-2')
|
11 |
+
# Call DetectDominantLanguage API
|
12 |
+
response = comprehend.detect_dominant_language(Text=text)
|
13 |
+
# Extract the detected language
|
14 |
+
detected_language = response['Languages'][0]['LanguageCode']
|
15 |
+
return detected_language
|
16 |
|
17 |
+
def translate_and_predict(text):
|
18 |
+
"""Translates text and generates an image using Stable Diffusion."""
|
19 |
+
source_language_code=detect_language(text)
|
20 |
+
# Translate input text
|
21 |
+
translate = boto3.client('translate', region_name='ap-northeast-2')
|
22 |
+
target_language_code="en"
|
23 |
+
print("source_language_code:" , source_language_code)
|
24 |
+
translation_result = translate.translate_text(Text=text, SourceLanguageCode=source_language_code, TargetLanguageCode=target_language_code)
|
25 |
+
translated_text = translation_result.get('TranslatedText')
|
26 |
+
print("translated_text:" , translated_text)
|
27 |
+
image_path=m(translated_text)
|
28 |
+
return image_path
|
29 |
|
30 |
+
# Define the Gradio interface
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=translate_and_predict, # Since we're only displaying an image, there's no function to call
|
33 |
+
inputs=["text"], # Input: text, source language, target language
|
34 |
+
outputs="image", # Output: generated image
|
35 |
+
title="Image Viewer",
|
36 |
+
)
|
37 |
|
38 |
+
# Launch the interface
|
39 |
+
iface.launch()
|