raannakasturi commited on
Commit
3f1a8fc
·
1 Parent(s): 9fb08e4

Enhance text handling in fix_text function and update Mindmap loading message for clarity

Browse files
Files changed (3) hide show
  1. image.py +75 -78
  2. main.py +3 -3
  3. post_blog.py +5 -6
image.py CHANGED
@@ -24,84 +24,81 @@ def fix_base64_padding(data):
24
  return data
25
 
26
  def generate_image(title, category, summary):
27
- print("Generating image...")
28
- import time
29
- start = time.time()
30
- try:
31
- try:
32
- try:
33
- negative="low quality, blurry, pixelated, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, poorly rendered hands, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, bad composition, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, unreal, animate, cgi, render, artwork, illustration, 3d render, cinema 4d, artstation, octane render, mutated body parts, painting, oil painting, 2d, sketch, bad photography, bad photo, deviant art, aberrations, abstract, anime, black and white, collapsed, conjoined, creative, drawing, extra windows, harsh lighting, jpeg artifacts, low saturation, monochrome, multiple levels, overexposed, oversaturated, photoshop, rotten, surreal, twisted, UI, underexposed, unnatural, unreal engine, unrealistic, video game, deformed body features, NSFW, NUDE, vulgar, negative, unsuitable, inappropriate, offensive, revealing, sexual, explicit",
34
- prompt = f"Generate accurate image representing the {category} concept: ```{title.strip()}: {summary.strip()}```"
35
- client = Client(
36
- image_provider=RetryProvider(
37
- providers=[ReplicateHome, Airforce],
38
- shuffle=True,
39
- single_provider_retry=True,
40
- max_retries=3,
41
- )
42
- )
43
- img_data = client.images.generate(
44
- model="sdxl",
45
- prompt=prompt,
46
- negative_prompt=negative,
47
- response_format="b64_json",
48
- width=1024,
49
- height=576,
50
- ).data[0].b64_json
51
- print(f"Image generated in {time.time() - start:.2f} seconds")
52
- if img_data:
53
- return f"data:image/png;base64,{img_data}"
54
- return None
55
- except Exception as e:
56
- print(f"Error generating image: {e}")
57
- negative="low quality, blurry, pixelated, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, poorly rendered hands, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, bad composition, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, unreal, animate, cgi, render, artwork, illustration, 3d render, cinema 4d, artstation, octane render, mutated body parts, painting, oil painting, 2d, sketch, bad photography, bad photo, deviant art, aberrations, abstract, anime, black and white, collapsed, conjoined, creative, drawing, extra windows, harsh lighting, jpeg artifacts, low saturation, monochrome, multiple levels, overexposed, oversaturated, photoshop, rotten, surreal, twisted, UI, underexposed, unnatural, unreal engine, unrealistic, video game, deformed body features, NSFW, NUDE, vulgar, negative, unsuitable, inappropriate, offensive, revealing, sexual, explicit",
58
- prompt = f"Generate accurate image representing the {category} concept: ```{title.strip()}: {summary.strip()}```"
59
- client = Client(
60
- image_provider=RetryProvider(
61
- providers=[Airforce, PollinationsAI, BlackboxCreateAgent],
62
- shuffle=True,
63
- single_provider_retry=True,
64
- max_retries=3,
65
- )
66
- )
67
- img_data = client.images.generate(
68
- model="flux",
69
- prompt=prompt,
70
- negative_prompt=negative,
71
- response_format="b64_json",
72
- width=1024,
73
- height=576,
74
- ).data[0].b64_json
75
- print(f"Image generated in {time.time() - start:.2f} seconds")
76
- if img_data:
77
- return f"data:image/png;base64,{img_data}"
78
- return None
79
- except Exception as e:
80
- print(f"Error generating image: {e}")
81
- negative="low quality, blurry, pixelated, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, poorly rendered hands, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, bad composition, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, unreal, animate, cgi, render, artwork, illustration, 3d render, cinema 4d, artstation, octane render, mutated body parts, painting, oil painting, 2d, sketch, bad photography, bad photo, deviant art, aberrations, abstract, anime, black and white, collapsed, conjoined, creative, drawing, extra windows, harsh lighting, jpeg artifacts, low saturation, monochrome, multiple levels, overexposed, oversaturated, photoshop, rotten, surreal, twisted, UI, underexposed, unnatural, unreal engine, unrealistic, video game, deformed body features, NSFW, NUDE, vulgar, negative, unsuitable, inappropriate, offensive, revealing, sexual, explicit",
82
- prompt = f"Generate accurate image representing the {category} concept: ```{title.strip()}: {summary.strip()}```"
83
- client = Client(
84
- image_provider=RetryProvider(
85
- providers=[BlackForestLabsFlux1Schnell, VoodoohopFlux1Schnell],
86
- shuffle=True,
87
- single_provider_retry=True,
88
- max_retries=3,
89
- )
90
- )
91
- img_data = client.images.generate(
92
- model="flux-schnell",
93
- prompt=prompt,
94
- negative_prompt=negative,
95
- response_format="b64_json",
96
- width=1024,
97
- height=576,
98
- ).data[0].b64_json
99
- print(f"Image generated in {time.time() - start:.2f} seconds")
100
- if img_data:
101
- return f"data:image/png;base64,{img_data}"
102
- return None
103
- except Exception as e:
104
- print(f"Error generating image: {e}")
105
  return None
106
 
107
  def verify_image(image_data):
 
24
  return data
25
 
26
  def generate_image(title, category, summary):
27
+ print("Generating image...")
28
+ import time
29
+ start = time.time()
30
+ # try:
31
+ # try:
32
+ # try:
33
+ # negative="low quality, blurry, pixelated, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, poorly rendered hands, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, bad composition, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, unreal, animate, cgi, render, artwork, illustration, 3d render, cinema 4d, artstation, octane render, mutated body parts, painting, oil painting, 2d, sketch, bad photography, bad photo, deviant art, aberrations, abstract, anime, black and white, collapsed, conjoined, creative, drawing, extra windows, harsh lighting, jpeg artifacts, low saturation, monochrome, multiple levels, overexposed, oversaturated, photoshop, rotten, surreal, twisted, UI, underexposed, unnatural, unreal engine, unrealistic, video game, deformed body features, NSFW, NUDE, vulgar, negative, unsuitable, inappropriate, offensive, revealing, sexual, explicit",
34
+ # prompt = f"Generate accurate image representing the {category} concept: ```{title.strip()}: {summary.strip()}```"
35
+ # client = Client()
36
+ # img_data = client.images.generate(
37
+ # provider=RetryProvider(
38
+ # providers=[ReplicateHome, Airforce],
39
+ # shuffle=True,
40
+ # single_provider_retry=True,
41
+ # max_retries=3,
42
+ # ),
43
+ # model="sdxl",
44
+ # prompt=prompt,
45
+ # negative_prompt=negative,
46
+ # response_format="b64_json",
47
+ # width=1024,
48
+ # height=576,
49
+ # ).data[0].b64_json
50
+ # print(f"Image generated in {time.time() - start:.2f} seconds")
51
+ # if img_data:
52
+ # return f"data:image/png;base64,{img_data}"
53
+ # return None
54
+ # except Exception as e:
55
+ # print(f"Error generating image: {e}")
56
+ # negative="low quality, blurry, pixelated, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, poorly rendered hands, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, bad composition, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, unreal, animate, cgi, render, artwork, illustration, 3d render, cinema 4d, artstation, octane render, mutated body parts, painting, oil painting, 2d, sketch, bad photography, bad photo, deviant art, aberrations, abstract, anime, black and white, collapsed, conjoined, creative, drawing, extra windows, harsh lighting, jpeg artifacts, low saturation, monochrome, multiple levels, overexposed, oversaturated, photoshop, rotten, surreal, twisted, UI, underexposed, unnatural, unreal engine, unrealistic, video game, deformed body features, NSFW, NUDE, vulgar, negative, unsuitable, inappropriate, offensive, revealing, sexual, explicit",
57
+ # prompt = f"Generate accurate image representing the {category} concept: ```{title.strip()}: {summary.strip()}```"
58
+ # client = Client()
59
+ # img_data = client.images.generate(
60
+ # provider=RetryProvider(
61
+ # providers=[Airforce, PollinationsAI, BlackboxCreateAgent],
62
+ # shuffle=True,
63
+ # single_provider_retry=True,
64
+ # max_retries=3,
65
+ # ),
66
+ # model="flux",
67
+ # prompt=prompt,
68
+ # negative_prompt=negative,
69
+ # response_format="b64_json",
70
+ # width=1024,
71
+ # height=576,
72
+ # ).data[0].b64_json
73
+ # print(f"Image generated in {time.time() - start:.2f} seconds")
74
+ # if img_data:
75
+ # return f"data:image/png;base64,{img_data}"
76
+ # return None
77
+ # except Exception as e:
78
+ # print(f"Error generating image: {e}")
79
+ # negative="low quality, blurry, pixelated, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, poorly rendered hands, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, bad composition, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, unreal, animate, cgi, render, artwork, illustration, 3d render, cinema 4d, artstation, octane render, mutated body parts, painting, oil painting, 2d, sketch, bad photography, bad photo, deviant art, aberrations, abstract, anime, black and white, collapsed, conjoined, creative, drawing, extra windows, harsh lighting, jpeg artifacts, low saturation, monochrome, multiple levels, overexposed, oversaturated, photoshop, rotten, surreal, twisted, UI, underexposed, unnatural, unreal engine, unrealistic, video game, deformed body features, NSFW, NUDE, vulgar, negative, unsuitable, inappropriate, offensive, revealing, sexual, explicit",
80
+ # prompt = f"Generate accurate image representing the {category} concept: ```{title.strip()}: {summary.strip()}```"
81
+ # client = Client()
82
+ # img_data = client.images.generate(
83
+ # provider=RetryProvider(
84
+ # providers=[BlackForestLabsFlux1Schnell, VoodoohopFlux1Schnell],
85
+ # shuffle=True,
86
+ # single_provider_retry=True,
87
+ # max_retries=3,
88
+ # ),
89
+ # model="flux-schnell",
90
+ # prompt=prompt,
91
+ # negative_prompt=negative,
92
+ # response_format="b64_json",
93
+ # width=1024,
94
+ # height=576,
95
+ # ).data[0].b64_json
96
+ # print(f"Image generated in {time.time() - start:.2f} seconds")
97
+ # if img_data:
98
+ # return f"data:image/png;base64,{img_data}"
99
+ # return None
100
+ # except Exception as e:
101
+ # print(f"Error generating image: {e}")
 
 
 
102
  return None
103
 
104
  def verify_image(image_data):
main.py CHANGED
@@ -18,7 +18,7 @@ def fix_text(text):
18
  fixed_text += word.encode('latin1').decode('utf-8')+" "
19
  except:
20
  fixed_text += word+" "
21
- return fixed_text.encode('utf-8').decode().replace('\\', '\\\\')
22
 
23
  def paper_data(paper_data, wait_time=5):
24
  data = {"status": "success"}
@@ -42,13 +42,13 @@ def paper_data(paper_data, wait_time=5):
42
  continue
43
  try:
44
  title = fix_text(title)
45
- summary = fix_text(summary)
46
- mindmap = fix_text(mindmap)
47
  citation = fix_text(citation)
48
  title = html.escape(str(title).strip())
49
  citation = html.escape(str(citation).strip())
50
  summary = html.escape(str(summary).strip())
 
51
  mindmap = html.escape(str(mindmap).strip())
 
52
  status = post_blog(doi, title, category, summary, mindmap, citation, access_key, wait_time)
53
  except Exception as e:
54
  print(f"Error posting blog '{title}': {e}")
 
18
  fixed_text += word.encode('latin1').decode('utf-8')+" "
19
  except:
20
  fixed_text += word+" "
21
+ return fixed_text.encode('utf-8').decode().replace("\n", "\\n").replace("\\n", "\\\\n").replace('\\', '\\\\')
22
 
23
  def paper_data(paper_data, wait_time=5):
24
  data = {"status": "success"}
 
42
  continue
43
  try:
44
  title = fix_text(title)
 
 
45
  citation = fix_text(citation)
46
  title = html.escape(str(title).strip())
47
  citation = html.escape(str(citation).strip())
48
  summary = html.escape(str(summary).strip())
49
+ print(summary)
50
  mindmap = html.escape(str(mindmap).strip())
51
+ print(mindmap)
52
  status = post_blog(doi, title, category, summary, mindmap, citation, access_key, wait_time)
53
  except Exception as e:
54
  print(f"Error posting blog '{title}': {e}")
post_blog.py CHANGED
@@ -31,8 +31,7 @@ def generate_post_html(doi, title, category, summary, mindmap, citation):
31
  </div>
32
  <br>
33
  <h2>Mindmap</h2>
34
- <p><small><em>If MindMap doesn't load, go to the <a href="/">Homepage</a> and visit blog again or <a
35
- href="/#">Switch to Android App (Under Development)</a>.</em></small></p>
36
  <div class="markmap" id="paper_mindmap">
37
  <script type="text/template">
38
  {mindmap.replace("&amp;", "&").replace(":", "=>").strip()}
@@ -116,10 +115,10 @@ def create_post(doi, title, category, summary, mindmap, citation):
116
  post_category = f"{category}"
117
  try:
118
  post_body, post_image = generate_post_html(doi, title, category, summary, mindmap, citation)
119
- # print("_____________________\n\n",title,"\n\n_____________________")
120
- # with open(f'{title.replace(" ", "_")}.html', 'w', encoding='utf-8') as f:
121
- # f.write(post_body)
122
- # exit()
123
  except Exception as e:
124
  print(f"Error generating post: {e}")
125
  return None, None, None, None
 
31
  </div>
32
  <br>
33
  <h2>Mindmap</h2>
34
+ <p><small><em>If MindMap doesn't load, please try refreshing the page.</em></small></p>
 
35
  <div class="markmap" id="paper_mindmap">
36
  <script type="text/template">
37
  {mindmap.replace("&amp;", "&").replace(":", "=>").strip()}
 
115
  post_category = f"{category}"
116
  try:
117
  post_body, post_image = generate_post_html(doi, title, category, summary, mindmap, citation)
118
+ print("_____________________\n\n",title,"\n\n_____________________")
119
+ with open(f'{title.replace(" ", "_")}.html', 'w', encoding='utf-8') as f:
120
+ f.write(post_body)
121
+ exit()
122
  except Exception as e:
123
  print(f"Error generating post: {e}")
124
  return None, None, None, None