Prakh24s commited on
Commit
45f69a5
·
verified ·
1 Parent(s): d002775

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -9
app.py CHANGED
@@ -22,16 +22,17 @@ def generate_image_openai(prompt):
22
 
23
  return response.data[0].url, response.data[0].revised_prompt
24
 
25
- def style_transfer(input_image_path, style_image_path, revised_prompt):
26
  input = {
27
  # "image": open(input_image_path, "rb"),
28
  "image": input_image_path,
29
  "image_style": open(style_image_path, "rb"),
30
  "style_strength": 0.4,
31
  "structure_strength":1.2,
32
- "negative_prompt": "hands, fingers, feet, legs",
33
- "prompt": " natural light, natural bright colors, low quality, candid, grainy, instagram photo, phone camera, candid, blurry home video, high iso noisy" ,
34
  "seed": 42,
 
35
  }
36
  output = replicate.run(
37
  "prakharsaxena24/2d-to-real-style:c0e1e612a11a13d1d57a6d647e7665ad850bc73715337c1f499bb7b52404c35a",
@@ -39,11 +40,12 @@ def style_transfer(input_image_path, style_image_path, revised_prompt):
39
  )
40
  return output[0]
41
 
42
- def upscale_image(image_path):
43
  input = {
44
  "image": image_path,
45
- "prompt": "candid photo, high iso, phone camera, grainy <lora:more_details:0.5> , symmetric hands" ,
46
- "scale_factor": 4,
 
47
 
48
  }
49
  output = replicate.run(
@@ -52,16 +54,40 @@ def upscale_image(image_path):
52
 
53
  return output[0]
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
 
58
  def infer(text,title):
59
 
60
- prompt = f"""Please create a simple suitable image to accompany the following text as part of an article with the title "{title}". The objects in the image must have realistic proportions. Text: "{text}"
61
  Please make sure not to include text in the image."""
62
  image_url_openai, revised_prompt = generate_image_openai(prompt)
63
- style_image_url = style_transfer(image_url_openai, f'./Style.png',revised_prompt)
64
- upscaled_image_url = upscale_image(style_image_url)
 
65
 
66
  response_dalle = requests.get(image_url_openai)
67
  dalle_img = Image.open(BytesIO(response_dalle.content))
 
22
 
23
  return response.data[0].url, response.data[0].revised_prompt
24
 
25
+ def style_transfer(input_image_path, style_image_path, prompt_det):
26
  input = {
27
  # "image": open(input_image_path, "rb"),
28
  "image": input_image_path,
29
  "image_style": open(style_image_path, "rb"),
30
  "style_strength": 0.4,
31
  "structure_strength":1.2,
32
+ "negative_prompt": "hands, fingers, feet, legs, shoes",
33
+ "prompt": " natural light, natural bright colors, low quality, candid, grainy, instagram photo, phone camera, high iso noisy "+prompt_det ,
34
  "seed": 42,
35
+ "guidance_scale": 5
36
  }
37
  output = replicate.run(
38
  "prakharsaxena24/2d-to-real-style:c0e1e612a11a13d1d57a6d647e7665ad850bc73715337c1f499bb7b52404c35a",
 
40
  )
41
  return output[0]
42
 
43
+ def upscale_image(image_path,prompt_det):
44
  input = {
45
  "image": image_path,
46
+ "prompt": "candid photo, high iso, phone camera, grainy <lora:more_details:0.5> , symmetric hands" + prompt_det,
47
+ "scale_factor": 3,
48
+ "negative_prompt": "hands, fingers, feet, legs, shoes",
49
 
50
  }
51
  output = replicate.run(
 
54
 
55
  return output[0]
56
 
57
+ def get_keyword_prompt(image_url):
58
+ client = OpenAI()
59
+ response = client.chat.completions.create(
60
+ model="gpt-4o",
61
+ messages=[
62
+ {
63
+ "role": "user",
64
+ "content": [
65
+ {"type": "text", "text": "Describe this image in detail, using phrases or keywords separated by commas. Include details about the person such as gender, race, and appearance excluding details about hair color, footwear. Indicate the position left or right. Keep it short and provide the information in one paragraph, separated by commas."},
66
+ {
67
+ "type": "image_url",
68
+ "image_url": {
69
+ "url": image_url,
70
+ },
71
+ },
72
+ ],
73
+ }
74
+ ],
75
+ # max_tokens=300,
76
+ )
77
+ print(response)
78
+ return response.choices[0].message.content
79
+
80
 
81
 
82
 
83
  def infer(text,title):
84
 
85
+ prompt = f"""Please create a simple suitable image to accompany the following text as part of an article with the title "{title}". The objects in the image must have realistic proportions, always keep people/person in focus. Text: "{text}"
86
  Please make sure not to include text in the image."""
87
  image_url_openai, revised_prompt = generate_image_openai(prompt)
88
+ prompt_det = get_keyword_prompt(image_url_openai)
89
+ style_image_url = style_transfer(image_url_openai, f'./Style.png',prompt_det)
90
+ upscaled_image_url = upscale_image(style_image_url, prompt_det)
91
 
92
  response_dalle = requests.get(image_url_openai)
93
  dalle_img = Image.open(BytesIO(response_dalle.content))