saranbalan commited on
Commit
c06d39e
1 Parent(s): 34ba4a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -7,22 +7,56 @@ import os
7
  import torch
8
  import openai
9
  from huggingface_hub import InferenceApi
10
-
11
-
 
 
12
 
13
  # Set up Groq API key
14
  api_key = os.getenv("GROQ_API_KEY")
15
  client = Groq(api_key=api_key)
16
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Set device: CUDA if available, else CPU
19
- # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
20
- # torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
 
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- model_id1 = os.getenv("API_KEY")
24
- pipe = StableDiffusionPipeline.from_pretrained(model_id1, low_cpu_mem_usage=True, use_safetensors=True)
25
- pipe = pipe.to('cpu')
26
 
27
  # Updated function for text generation using the new API structure
28
  def generate_creative_text(prompt):
@@ -40,8 +74,6 @@ def process_audio(audio_path, image_option, creative_text_option):
40
  if audio_path is None:
41
  return "Please upload an audio file.", None, None, None
42
 
43
-
44
-
45
  # Step 1: Transcribe audio
46
  try:
47
  with open(audio_path, "rb") as file:
@@ -67,20 +99,15 @@ def process_audio(audio_path, image_option, creative_text_option):
67
  if creative_text_option == "Generate Creative Text":
68
  creative_text = generate_creative_text(translation)
69
 
70
-
71
  # Step 4: Generate image (if selected)
72
  image = None
73
  if image_option == "Generate Image":
74
- try:
75
- model_id1 = "dreamlike-art/dreamlike-diffusion-1.0"
76
- pipe = StableDiffusionPipeline.from_pretrained(model_id1, torch_dtype=torch.float16, use_safetensors=True)
77
- pipe = pipe.to('cpu')
78
- image = pipe(translation).images[0]
79
- except Exception as e:
80
- return tamil_text, translation, creative_text, f"An error occurred during image generation: {str(e)}"
81
 
82
  return tamil_text, translation, creative_text, image
83
-
84
 
85
  # Create Gradio interface
86
  with gr.Blocks(theme=gr.themes.Base()) as iface:
@@ -104,4 +131,3 @@ with gr.Blocks(theme=gr.themes.Base()) as iface:
104
 
105
  # Launch the interface
106
  iface.launch()
107
-
 
7
  import torch
8
  import openai
9
  from huggingface_hub import InferenceApi
10
+ from PIL import Image
11
+ import requests
12
+ import io
13
+ import time
14
 
15
  # Set up Groq API key
16
  api_key = os.getenv("GROQ_API_KEY")
17
  client = Groq(api_key=api_key)
18
 
19
+ # Hugging Face API details for image generation
20
+ H_key = os.getenv('API_KEY')
21
+ API_URL = "https://api-inference.huggingface.co/models/Artples/LAI-ImageGeneration-vSDXL-2"
22
+ headers = {"Authorization": f"Bearer {H_key}"}
23
+
24
+
25
+ # Function for querying image generation with retries
26
+ def query_image_generation(payload, max_retries=5):
27
+ for attempt in range(max_retries):
28
+ response = requests.post(API_URL, headers=headers, json=payload)
29
 
30
+ if response.status_code == 503:
31
+ print(f"Model is still loading, retrying... Attempt {attempt + 1}/{max_retries}")
32
+ estimated_time = min(response.json().get("estimated_time", 60), 60)
33
+ time.sleep(estimated_time)
34
+ continue
35
 
36
+ if response.status_code != 200:
37
+ print(f"Error: Received status code {response.status_code}")
38
+ print(f"Response: {response.text}")
39
+ return None
40
+
41
+ return response.content
42
+
43
+ print(f"Failed to generate image after {max_retries} attempts.")
44
+ return None
45
+
46
+ # Function for generating an image from text
47
+ def generate_image(prompt):
48
+ image_bytes = query_image_generation({"inputs": prompt})
49
+
50
+ if image_bytes is None:
51
+ return None
52
+
53
+ try:
54
+ image = Image.open(io.BytesIO(image_bytes)) # Opening the image from bytes
55
+ return image
56
+ except Exception as e:
57
+ print(f"Error: {e}")
58
+ return None
59
 
 
 
 
60
 
61
  # Updated function for text generation using the new API structure
62
  def generate_creative_text(prompt):
 
74
  if audio_path is None:
75
  return "Please upload an audio file.", None, None, None
76
 
 
 
77
  # Step 1: Transcribe audio
78
  try:
79
  with open(audio_path, "rb") as file:
 
99
  if creative_text_option == "Generate Creative Text":
100
  creative_text = generate_creative_text(translation)
101
 
 
102
  # Step 4: Generate image (if selected)
103
  image = None
104
  if image_option == "Generate Image":
105
+ image = generate_image(translation)
106
+ if image is None:
107
+ return tamil_text, translation, creative_text, f"An error occurred during image generation"
 
 
 
 
108
 
109
  return tamil_text, translation, creative_text, image
110
+
111
 
112
  # Create Gradio interface
113
  with gr.Blocks(theme=gr.themes.Base()) as iface:
 
131
 
132
  # Launch the interface
133
  iface.launch()