Rahatara commited on
Commit
f43f151
·
verified ·
1 Parent(s): a9e5020

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -72
app.py CHANGED
@@ -1,68 +1,11 @@
1
- import gradio as gr
2
- import replicate
3
- import os
4
- import random
5
- import openai
6
- import numpy as np
7
- from PIL import Image
8
- import requests
9
  import io
10
  import base64
 
 
 
 
11
  import zipfile
12
 
13
- # Set API tokens
14
- os.environ["REPLICATE_API_TOKEN"] = "r8_Brv0MtpmAiqrXrMrziyUXoSHuFV5hqs1Lw4Mo"
15
- # Initialize the Replicate client
16
- rep_client = replicate.Client()
17
-
18
- # Set your OpenAI API key
19
- OPENAI_API_KEY = "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"
20
- openai.api_key = OPENAI_API_KEY
21
-
22
- # Predefined prompts for the dropdown
23
- predefined_prompts = [
24
- "Missing bolts on railway track",
25
- "Cracks on railway track",
26
- "Overgrown vegetation near railway track",
27
- "Broken railings on railway bridge",
28
- "Debris on railway track",
29
- "Damaged railway platform"
30
- ]
31
-
32
- def ask_rail_defect_question(question, model_name='ft:gpt-3.5-turbo-0125:personal::99NsSAeQ'):
33
- response = openai.ChatCompletion.create(
34
- model=model_name,
35
- messages=[
36
- {
37
- "role": "system",
38
- "content": "The assistant is knowledgeable about rail defects and can answer questions related to them.",
39
- },
40
- {
41
- "role": "user",
42
- "content": question,
43
- }
44
- ],
45
- )
46
- return response.choices[0].message['content']
47
-
48
- # Function to generate variations enhanced by the GPT model
49
- def generate_variations(base_prompt, number_of_variations):
50
- locations = ["on the left side", "on the right side", "at the top", "at the bottom", "in the center"]
51
- sizes = ["small", "medium", "large", "tiny", "huge"]
52
- weather_conditions = ["under cold conditions", "during hot weather", "in dry weather", "in humid conditions", "under varying temperatures"]
53
-
54
- variations = []
55
- for _ in range(number_of_variations):
56
- location = random.choice(locations)
57
- size = random.choice(sizes)
58
- weather = random.choice(weather_conditions)
59
-
60
- # Enhance the base prompt with the GPT model
61
- enhanced_prompt = base_prompt
62
- full_prompt = f"{enhanced_prompt}, with a {size} defect {location}, observed {weather}."
63
- variations.append(full_prompt)
64
- return variations
65
-
66
  def image_to_data_url(image):
67
  buffered = io.BytesIO()
68
  image.save(buffered, format="PNG")
@@ -78,7 +21,7 @@ def inpaint_defect(image, prompt, num_images=1):
78
  images = []
79
 
80
  for _ in range(num_images):
81
- input = {
82
  "input_image": image_data_url,
83
  "instruction_text": prompt,
84
  "scheduler": "K_EULER_ANCESTRAL",
@@ -90,16 +33,25 @@ def inpaint_defect(image, prompt, num_images=1):
90
 
91
  prediction = rep_client.predictions.create(
92
  version="10e63b0e6361eb23a0374f4d9ee145824d9d09f7a31dcd70803193ebc7121430",
93
- input=input
94
  )
95
  prediction.wait()
 
96
  if prediction.status == "succeeded":
97
  image_url = prediction.output[0]
98
- response = requests.get(image_url)
99
- img = Image.open(io.BytesIO(response.content))
100
- images.append(img)
 
 
 
 
 
 
101
  else:
 
102
  images.append(None)
 
103
  return images
104
 
105
  # Function to generate images from prompts
@@ -129,10 +81,11 @@ def download_images_as_zip(images):
129
  zip_buffer = io.BytesIO()
130
  with zipfile.ZipFile(zip_buffer, 'w') as zf:
131
  for i, img in enumerate(images):
132
- img_buffer = io.BytesIO()
133
- img.save(img_buffer, format='PNG')
134
- img_buffer.seek(0)
135
- zf.writestr(f'image_{i + 1}.png', img_buffer.read())
 
136
  zip_buffer.seek(0)
137
  return zip_buffer
138
 
@@ -202,5 +155,4 @@ with gr.Blocks() as app:
202
  outputs=gr.File(label="Download Zip")
203
  )
204
 
205
- if __name__ == "__main__":
206
- app.launch()
 
 
 
 
 
 
 
 
 
1
  import io
2
  import base64
3
+ import requests
4
+ import numpy as np
5
+ from PIL import Image
6
+ import gradio as gr
7
  import zipfile
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def image_to_data_url(image):
10
  buffered = io.BytesIO()
11
  image.save(buffered, format="PNG")
 
21
  images = []
22
 
23
  for _ in range(num_images):
24
+ input_data = {
25
  "input_image": image_data_url,
26
  "instruction_text": prompt,
27
  "scheduler": "K_EULER_ANCESTRAL",
 
33
 
34
  prediction = rep_client.predictions.create(
35
  version="10e63b0e6361eb23a0374f4d9ee145824d9d09f7a31dcd70803193ebc7121430",
36
+ input=input_data
37
  )
38
  prediction.wait()
39
+
40
  if prediction.status == "succeeded":
41
  image_url = prediction.output[0]
42
+ print(f"Generated Image URL: {image_url}") # Debugging line to check the URL
43
+ try:
44
+ response = requests.get(image_url)
45
+ response.raise_for_status() # Check for HTTP errors
46
+ img = Image.open(io.BytesIO(response.content))
47
+ images.append(img)
48
+ except requests.exceptions.RequestException as e:
49
+ print(f"Error fetching image from URL: {e}")
50
+ images.append(None)
51
  else:
52
+ print(f"Prediction failed: {prediction.status}")
53
  images.append(None)
54
+
55
  return images
56
 
57
  # Function to generate images from prompts
 
81
  zip_buffer = io.BytesIO()
82
  with zipfile.ZipFile(zip_buffer, 'w') as zf:
83
  for i, img in enumerate(images):
84
+ if img is not None:
85
+ img_buffer = io.BytesIO()
86
+ img.save(img_buffer, format='PNG')
87
+ img_buffer.seek(0)
88
+ zf.writestr(f'image_{i + 1}.png', img_buffer.read())
89
  zip_buffer.seek(0)
90
  return zip_buffer
91
 
 
155
  outputs=gr.File(label="Download Zip")
156
  )
157
 
158
+ app.launch()