randeom commited on
Commit
bce356e
·
verified ·
1 Parent(s): 73631f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -29
app.py CHANGED
@@ -5,8 +5,8 @@ from PIL import Image
5
  import requests
6
  from io import BytesIO
7
  import re
8
- import pandas as pd
9
 
 
10
  def fetch_api_endpoint(url):
11
  try:
12
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
@@ -15,7 +15,7 @@ def fetch_api_endpoint(url):
15
 
16
  # Use regular expressions to find the endpoint URL in the script content
17
  endpoint_match = re.search(r'root":"(https://[^"]+)', response.text)
18
- if (endpoint_match):
19
  endpoint_url = endpoint_match.group(1)
20
  return endpoint_url
21
  else:
@@ -27,15 +27,19 @@ def fetch_api_endpoint(url):
27
  print("An unexpected error occurred:", e)
28
  return None
29
 
 
30
  url = "https://playgroundai-playground-v2-5.hf.space/"
31
 
 
32
  endpoint = fetch_api_endpoint(url)
33
 
34
  if endpoint is None:
35
  print("Failed to fetch the API endpoint.")
36
  exit(1)
37
 
 
38
  client_mistral = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
 
39
  client_playground = Client(endpoint)
40
 
41
  def format_prompt(logo_request):
@@ -102,20 +106,11 @@ css = """
102
  }
103
  """
104
 
105
- results = []
106
-
107
  def process_request(logo_request, width, height, guidance_scale):
108
  improved_prompt = generate_improved_prompt(logo_request)
109
  image = generate_image(improved_prompt, width, height, guidance_scale)
110
- results.append({"Prompt": logo_request, "Guidance Scale": guidance_scale, "Size": f"{width}x{height}", "Image": image})
111
- display_results()
112
  return improved_prompt, image
113
 
114
- def display_results():
115
- df = pd.DataFrame(results)
116
- html = df.to_html(escape=False, formatters=dict(Image=lambda x: x._repr_html_()))
117
- display_html(html, raw=True)
118
-
119
  with gr.Blocks(css=css) as app:
120
  with gr.Row():
121
  with gr.Column(scale=2):
@@ -124,9 +119,6 @@ with gr.Blocks(css=css) as app:
124
  width = gr.Slider(label="Width", minimum=256, maximum=1536, value=1024)
125
  height = gr.Slider(label="Height", minimum=256, maximum=1536, value=1024)
126
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=20, value=3)
127
- example1 = gr.Button("Example 1: Abstract geometric shape")
128
- example2 = gr.Button("Example 2: Minimalist animal logo")
129
- example3 = gr.Button("Example 3: Futuristic tech icon")
130
  with gr.Column(scale=3):
131
  gr.HTML("<h1><center>Magical AI Logo Generator<h1><center>")
132
  generate_button = gr.Button("Generate")
@@ -137,21 +129,6 @@ with gr.Blocks(css=css) as app:
137
  inputs=[logo_input, width, height, guidance_scale],
138
  outputs=[prompt_output, image_output]
139
  )
140
- example1.click(
141
- fn=lambda: ("Abstract geometric shape",),
142
- inputs=None,
143
- outputs=[logo_input]
144
- )
145
- example2.click(
146
- fn=lambda: ("Minimalist animal logo",),
147
- inputs=None,
148
- outputs=[logo_input]
149
- )
150
- example3.click(
151
- fn=lambda: ("Futuristic tech icon",),
152
- inputs=None,
153
- outputs=[logo_input]
154
- )
155
 
156
  gr.Markdown("""
157
  ---
 
5
  import requests
6
  from io import BytesIO
7
  import re
 
8
 
9
+ # Function to fetch the API endpoint from the URL using regular expressions
10
  def fetch_api_endpoint(url):
11
  try:
12
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
 
15
 
16
  # Use regular expressions to find the endpoint URL in the script content
17
  endpoint_match = re.search(r'root":"(https://[^"]+)', response.text)
18
+ if endpoint_match:
19
  endpoint_url = endpoint_match.group(1)
20
  return endpoint_url
21
  else:
 
27
  print("An unexpected error occurred:", e)
28
  return None
29
 
30
+ # Define the URL to fetch the API endpoint from
31
  url = "https://playgroundai-playground-v2-5.hf.space/"
32
 
33
+ # Fetch the API endpoint
34
  endpoint = fetch_api_endpoint(url)
35
 
36
  if endpoint is None:
37
  print("Failed to fetch the API endpoint.")
38
  exit(1)
39
 
40
+ # Initialize the HuggingFace Inference Client with the specified model
41
  client_mistral = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
42
+ # Initialize the Playground AI client with the fetched endpoint
43
  client_playground = Client(endpoint)
44
 
45
  def format_prompt(logo_request):
 
106
  }
107
  """
108
 
 
 
109
  def process_request(logo_request, width, height, guidance_scale):
110
  improved_prompt = generate_improved_prompt(logo_request)
111
  image = generate_image(improved_prompt, width, height, guidance_scale)
 
 
112
  return improved_prompt, image
113
 
 
 
 
 
 
114
  with gr.Blocks(css=css) as app:
115
  with gr.Row():
116
  with gr.Column(scale=2):
 
119
  width = gr.Slider(label="Width", minimum=256, maximum=1536, value=1024)
120
  height = gr.Slider(label="Height", minimum=256, maximum=1536, value=1024)
121
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=20, value=3)
 
 
 
122
  with gr.Column(scale=3):
123
  gr.HTML("<h1><center>Magical AI Logo Generator<h1><center>")
124
  generate_button = gr.Button("Generate")
 
129
  inputs=[logo_input, width, height, guidance_scale],
130
  outputs=[prompt_output, image_output]
131
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  gr.Markdown("""
134
  ---