Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,16 @@ from PIL import Image
|
|
5 |
import torch
|
6 |
import io
|
7 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Check if GPU is available
|
9 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
|
@@ -20,11 +30,6 @@ generator_tokenizer = AutoTokenizer.from_pretrained(generator_model)
|
|
20 |
if generator_tokenizer.pad_token is None:
|
21 |
generator_tokenizer.pad_token = generator_tokenizer.eos_token
|
22 |
|
23 |
-
# Hugging Face API for Image Generation
|
24 |
-
HF_API_KEY = os.getenv("HF_API_KEY")
|
25 |
-
IMAGE_GEN_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
26 |
-
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
27 |
-
|
28 |
def translate_tamil_to_english(text):
|
29 |
"""Translates Tamil text to English."""
|
30 |
inputs = translator_tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device)
|
@@ -42,7 +47,7 @@ def generate_image(prompt):
|
|
42 |
response = requests.post(IMAGE_GEN_URL, headers=HEADERS, json={"inputs": prompt})
|
43 |
if response.status_code == 200:
|
44 |
return Image.open(io.BytesIO(response.content))
|
45 |
-
return Image.new("RGB", (300, 300), "red") #
|
46 |
|
47 |
def process_input(tamil_text):
|
48 |
"""Complete pipeline: Translation, Text Generation, and Image Generation."""
|
|
|
5 |
import torch
|
6 |
import io
|
7 |
import os
|
8 |
+
|
9 |
+
# Load Hugging Face API key securely
|
10 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
11 |
+
if not HF_API_KEY:
|
12 |
+
raise ValueError("HF_API_KEY is not set. Add it in Hugging Face 'Variables and Secrets'.")
|
13 |
+
|
14 |
+
# API Endpoint for Image Generation
|
15 |
+
IMAGE_GEN_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
16 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}
|
17 |
+
|
18 |
# Check if GPU is available
|
19 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
20 |
|
|
|
30 |
if generator_tokenizer.pad_token is None:
|
31 |
generator_tokenizer.pad_token = generator_tokenizer.eos_token
|
32 |
|
|
|
|
|
|
|
|
|
|
|
33 |
def translate_tamil_to_english(text):
|
34 |
"""Translates Tamil text to English."""
|
35 |
inputs = translator_tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device)
|
|
|
47 |
response = requests.post(IMAGE_GEN_URL, headers=HEADERS, json={"inputs": prompt})
|
48 |
if response.status_code == 200:
|
49 |
return Image.open(io.BytesIO(response.content))
|
50 |
+
return Image.new("RGB", (300, 300), "red") # Placeholder image for errors
|
51 |
|
52 |
def process_input(tamil_text):
|
53 |
"""Complete pipeline: Translation, Text Generation, and Image Generation."""
|