Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,18 +13,21 @@ from google_auth_oauthlib.flow import InstalledAppFlow
|
|
| 13 |
from google.auth.transport.requests import Request
|
| 14 |
from datetime import datetime
|
| 15 |
from ascii_magic import AsciiArt
|
|
|
|
| 16 |
|
| 17 |
-
# Stable Diffusion setup
|
| 18 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 19 |
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# BLIP setup for image captioning
|
| 22 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 23 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 24 |
|
| 25 |
# Paths for Google OAuth
|
| 26 |
-
TOKEN_PATH = "token.json"
|
| 27 |
-
CLIENT_SECRET_PATH = "
|
| 28 |
API_URL = "https://generativelanguage.googleapis.com/v1beta/tunedModels/csa-10-he0op144r76e:generateContent"
|
| 29 |
|
| 30 |
def load_credentials():
|
|
@@ -79,21 +82,11 @@ def is_image_generation_request(text: str) -> bool:
|
|
| 79 |
image_keywords = [
|
| 80 |
"tạo ảnh", "Tạo Ảnh", "TẠO ẢNH", "tạo hình", "Tạo Hình", "TẠO HÌNH",
|
| 81 |
"vẽ", "Vẽ", "VẼ", "hình ảnh", "Hình Ảnh", "HÌNH ẢNH", "ảnh của", "Ảnh Của", "ẢNH CỦA",
|
| 82 |
-
"
|
| 83 |
-
"phát sinh ảnh", "Phát Sinh Ảnh", "PHÁT SINH ẢNH",
|
| 84 |
-
"ảnh kỹ thuật số", "Ảnh Kỹ Thuật Số", "ẢNH KỸ THUẬT SỐ",
|
| 85 |
-
"dựng hình", "Dựng Hình", "DỰNG HÌNH",
|
| 86 |
-
"thiết kế hình ảnh", "Thiết Kế Hình Ảnh", "THIẾT KẾ HÌNH ẢNH",
|
| 87 |
-
"tạo đồ họa", "Tạo Đồ Họa", "TẠO ĐỒ HỌA", "Tạo cho tôi ảnh",
|
| 88 |
-
"generate image", "Generate Image", "GENERATE IMAGE",
|
| 89 |
-
"image generation", "Image Generation", "IMAGE GENERATION",
|
| 90 |
-
"draw", "Draw", "DRAW", "picture", "Picture", "PICTURE",
|
| 91 |
-
"image of", "Image Of", "IMAGE OF", "create image", "Create Image", "CREATE IMAGE"
|
| 92 |
]
|
| 93 |
text_lower = text.lower()
|
| 94 |
return any(keyword in text_lower for keyword in image_keywords)
|
| 95 |
|
| 96 |
-
# Hàm xử lý cho /generate
|
| 97 |
def generate_content(text):
|
| 98 |
access_token = get_access_token()
|
| 99 |
headers = {
|
|
@@ -110,19 +103,17 @@ def generate_content(text):
|
|
| 110 |
image = generate_image(prompt)
|
| 111 |
ascii_art = AsciiArt.from_pillow_image(image)
|
| 112 |
print(ascii_art.to_ascii())
|
| 113 |
-
return image, ascii_art.to_ascii()
|
| 114 |
else:
|
| 115 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 116 |
-
return response.json()
|
| 117 |
|
| 118 |
-
# Hàm xử lý cho /generate-image
|
| 119 |
def generate_image_only(prompt):
|
| 120 |
image = generate_image(prompt)
|
| 121 |
ascii_art = AsciiArt.from_pillow_image(image)
|
| 122 |
print(ascii_art.to_ascii())
|
| 123 |
return image, ascii_art.to_ascii()
|
| 124 |
|
| 125 |
-
# Hàm xử lý cho /upload-image
|
| 126 |
def upload_and_process(image, prompt):
|
| 127 |
inputs = processor(images=image, return_tensors="pt")
|
| 128 |
with torch.no_grad():
|
|
@@ -134,12 +125,17 @@ def upload_and_process(image, prompt):
|
|
| 134 |
print(ascii_art.to_ascii())
|
| 135 |
return generated_image, caption, ascii_art.to_ascii()
|
| 136 |
|
| 137 |
-
|
| 138 |
-
def speech_to_text(audio):
|
| 139 |
recognizer = sr.Recognizer()
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
text = recognizer.recognize_google(audio_content, language="vi-VN")
|
| 144 |
return generate_content(text)
|
| 145 |
|
|
@@ -181,7 +177,7 @@ with gr.Blocks(title="Ứng dụng AI với Gradio") as app:
|
|
| 181 |
)
|
| 182 |
|
| 183 |
with gr.Tab("Speech to Text"):
|
| 184 |
-
audio_input = gr.Audio(
|
| 185 |
audio_text_output = gr.JSON(label="Kết quả văn bản")
|
| 186 |
audio_image_output = gr.Image(label="Ảnh tạo ra (nếu có)")
|
| 187 |
audio_ascii_output = gr.Textbox(label="ASCII Art (nếu có)")
|
|
|
|
| 13 |
from google.auth.transport.requests import Request
|
| 14 |
from datetime import datetime
|
| 15 |
from ascii_magic import AsciiArt
|
| 16 |
+
import pyaudio
|
| 17 |
|
| 18 |
+
# Stable Diffusion setup với accelerate
|
| 19 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 20 |
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 21 |
+
if torch.cuda.is_available():
|
| 22 |
+
pipe = pipe.to("cuda") # Dùng GPU nếu có
|
| 23 |
|
| 24 |
# BLIP setup for image captioning
|
| 25 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 26 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 27 |
|
| 28 |
# Paths for Google OAuth
|
| 29 |
+
TOKEN_PATH = "/Users/hoangkha/Documents/website/BE/token.json"
|
| 30 |
+
CLIENT_SECRET_PATH = "/Users/hoangkha/Documents/website/BE/client_secret_589296761620-lqo0vs89j6c12link1tgjao99rnoaenk.apps.googleusercontent.com.json"
|
| 31 |
API_URL = "https://generativelanguage.googleapis.com/v1beta/tunedModels/csa-10-he0op144r76e:generateContent"
|
| 32 |
|
| 33 |
def load_credentials():
|
|
|
|
| 82 |
image_keywords = [
|
| 83 |
"tạo ảnh", "Tạo Ảnh", "TẠO ẢNH", "tạo hình", "Tạo Hình", "TẠO HÌNH",
|
| 84 |
"vẽ", "Vẽ", "VẼ", "hình ảnh", "Hình Ảnh", "HÌNH ẢNH", "ảnh của", "Ảnh Của", "ẢNH CỦA",
|
| 85 |
+
"generate image", "Generate Image", "GENERATE IMAGE"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
]
|
| 87 |
text_lower = text.lower()
|
| 88 |
return any(keyword in text_lower for keyword in image_keywords)
|
| 89 |
|
|
|
|
| 90 |
def generate_content(text):
|
| 91 |
access_token = get_access_token()
|
| 92 |
headers = {
|
|
|
|
| 103 |
image = generate_image(prompt)
|
| 104 |
ascii_art = AsciiArt.from_pillow_image(image)
|
| 105 |
print(ascii_art.to_ascii())
|
| 106 |
+
return image, ascii_art.to_ascii(), None
|
| 107 |
else:
|
| 108 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 109 |
+
return None, None, response.json()
|
| 110 |
|
|
|
|
| 111 |
def generate_image_only(prompt):
|
| 112 |
image = generate_image(prompt)
|
| 113 |
ascii_art = AsciiArt.from_pillow_image(image)
|
| 114 |
print(ascii_art.to_ascii())
|
| 115 |
return image, ascii_art.to_ascii()
|
| 116 |
|
|
|
|
| 117 |
def upload_and_process(image, prompt):
|
| 118 |
inputs = processor(images=image, return_tensors="pt")
|
| 119 |
with torch.no_grad():
|
|
|
|
| 125 |
print(ascii_art.to_ascii())
|
| 126 |
return generated_image, caption, ascii_art.to_ascii()
|
| 127 |
|
| 128 |
+
def speech_to_text(audio=None):
|
|
|
|
| 129 |
recognizer = sr.Recognizer()
|
| 130 |
+
if audio:
|
| 131 |
+
with sr.AudioFile(audio) as source:
|
| 132 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 133 |
+
audio_content = recognizer.record(source)
|
| 134 |
+
else:
|
| 135 |
+
with sr.Microphone() as source:
|
| 136 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 137 |
+
print("Đang nghe...")
|
| 138 |
+
audio_content = recognizer.listen(source)
|
| 139 |
text = recognizer.recognize_google(audio_content, language="vi-VN")
|
| 140 |
return generate_content(text)
|
| 141 |
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
with gr.Tab("Speech to Text"):
|
| 180 |
+
audio_input = gr.Audio(type="filepath", label="Ghi âm từ micro hoặc tải file")
|
| 181 |
audio_text_output = gr.JSON(label="Kết quả văn bản")
|
| 182 |
audio_image_output = gr.Image(label="Ảnh tạo ra (nếu có)")
|
| 183 |
audio_ascii_output = gr.Textbox(label="ASCII Art (nếu có)")
|