pravin0077 commited on
Commit
9051efa
·
verified ·
1 Parent(s): 39ba994

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  from PIL import Image
4
  import io
5
  import gradio as gr
 
6
 
7
  # Load the MarianMT model and tokenizer for translation (Tamil to English)
8
  model_name = "Helsinki-NLP/opus-mt-mul-en"
@@ -20,12 +21,15 @@ if text_generation_tokenizer.pad_token is None:
20
 
21
  # Hugging Face API for FLUX.1 image generation
22
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
23
- headers = {"Authorization": "HUGGINGFACE_API_KEY"} # Replace with your API key
24
 
25
  # Query Hugging Face API to generate image
26
  def query(payload):
27
  response = requests.post(API_URL, headers=headers, json=payload)
28
- return response.content
 
 
 
29
 
30
  # Translate Tamil text to English
31
  def translate_text(tamil_text):
@@ -37,6 +41,8 @@ def translate_text(tamil_text):
37
  # Generate an image based on the translated text
38
  def generate_image(prompt):
39
  image_bytes = query({"inputs": prompt})
 
 
40
  image = Image.open(io.BytesIO(image_bytes))
41
  return image
42
 
@@ -74,4 +80,4 @@ interface = gr.Interface(
74
  )
75
 
76
  # Launch Gradio app
77
- interface.launch()
 
3
  from PIL import Image
4
  import io
5
  import gradio as gr
6
+ import os
7
 
8
  # Load the MarianMT model and tokenizer for translation (Tamil to English)
9
  model_name = "Helsinki-NLP/opus-mt-mul-en"
 
21
 
22
  # Hugging Face API for FLUX.1 image generation
23
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
24
+ headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
25
 
26
  # Query Hugging Face API to generate image
27
  def query(payload):
28
  response = requests.post(API_URL, headers=headers, json=payload)
29
+ if response.status_code == 200:
30
+ return response.content
31
+ else:
32
+ return f"Error: {response.status_code} - {response.text}"
33
 
34
  # Translate Tamil text to English
35
  def translate_text(tamil_text):
 
41
  # Generate an image based on the translated text
42
  def generate_image(prompt):
43
  image_bytes = query({"inputs": prompt})
44
+ if isinstance(image_bytes, str) and "Error" in image_bytes:
45
+ return image_bytes # Return the error message if there's a problem with the API call
46
  image = Image.open(io.BytesIO(image_bytes))
47
  return image
48
 
 
80
  )
81
 
82
  # Launch Gradio app
83
+ interface.launch()