multimodalart HF staff commited on
Commit
2a43fc6
1 Parent(s): 65351e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -36
app.py CHANGED
@@ -6,14 +6,7 @@ from slugify import slugify
6
  import gradio as gr
7
  import uuid
8
  from typing import Optional
9
-
10
- from selenium import webdriver
11
- from webdriver_manager.chrome import ChromeDriverManager
12
- from selenium.webdriver.common.by import By
13
- from selenium.webdriver.support.ui import WebDriverWait
14
- from selenium.webdriver.support import expected_conditions as EC
15
-
16
- driver = webdriver.Chrome(ChromeDriverManager().install())
17
 
18
 
19
  def get_json_data(url):
@@ -148,35 +141,54 @@ def upload_civit_to_hf(profile: Optional[gr.OAuthProfile], url, progress=gr.Prog
148
  raise gr.Error("something went wrong")
149
  return "Model uploaded!"
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  def check_civit_link(profile: Optional[gr.OAuthProfile], url):
152
  info, _ = process_url(url, download_files=False)
153
- url_creator = f"https://civitai.com/user/{info['creator']}/models"
154
- # Open the target URL
155
- driver.get(url_creator)
156
-
157
- # Define the XPath expression
158
- xpath_expression = "//a[contains(@class, 'mantine-UnstyledButton-root') and contains(@class, 'mantine-ActionIcon-root') and contains(@class, 'mantine-ubxmi3') and starts-with(@href, 'https://huggingface.co/')]"
159
-
160
- # Find the element using the XPath expression
161
- try:
162
- element = WebDriverWait(driver, 10).until(
163
- EC.presence_of_element_located((By.XPATH, xpath_expression))
164
- )
165
-
166
- # Extract the href attribute
167
- href = element.get_attribute("href")
168
 
169
- # Extract the part after 'https://huggingface.co/'
170
- extracted_part = href.replace("https://huggingface.co/", "")
171
-
172
- except Exception as e:
173
- print("Element not found or error occurred:", e)
174
-
175
- finally:
176
- driver.quit()
177
-
178
- return extracted_part == profile.name
179
-
180
  def swap_fill(profile: Optional[gr.OAuthProfile]):
181
  if profile is None:
182
  return gr.update(visible=True), gr.update(visible=False)
@@ -226,11 +238,11 @@ with gr.Blocks(css=css) as demo:
226
  )
227
  #is_author = gr.Checkbox(label="Are you the model author?", info="If you are not the author, a disclaimer with information about the author and the CivitAI source will be added", value=False)
228
  instructions = gr.HTML("")
229
- submit_button_civit = gr.Button("Upload model to Hugging Face")
230
  output = gr.Textbox(label="Output progress")
231
 
232
  demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
233
- submit_source_civit.change(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions])
234
  submit_button_civit.click(fn=upload_civit_to_hf, inputs=[submit_source_civit], outputs=[output])
235
 
236
  demo.queue()
 
6
  import gradio as gr
7
  import uuid
8
  from typing import Optional
9
+ import json
 
 
 
 
 
 
 
10
 
11
 
12
  def get_json_data(url):
 
141
  raise gr.Error("something went wrong")
142
  return "Model uploaded!"
143
 
144
+
145
+ def get_creator(username):
146
+ url = f"https://civitai.com/api/trpc/user.getCreator?input=%7B%22json%22%3A%7B%22username%22%3A%22{username}%22%2C%22authed%22%3Atrue%7D%7D"
147
+ headers = {
148
+ "authority": "civitai.com",
149
+ "accept": "*/*",
150
+ "accept-language": "en-BR,en;q=0.9,pt-BR;q=0.8,pt;q=0.7,es-ES;q=0.6,es;q=0.5,de-LI;q=0.4,de;q=0.3,en-GB;q=0.2,en-US;q=0.1,sk;q=0.1",
151
+ "content-type": "application/json",
152
+ "cookie": f"{os.environ["COOKIE_INFO"]}",
153
+ "if-modified-since": "Tue, 22 Aug 2023 07:18:52 GMT",
154
+ "referer": f"https://civitai.com/user/{username}/models",
155
+ "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
156
+ "sec-ch-ua-mobile": "?0",
157
+ "sec-ch-ua-platform": "macOS",
158
+ "sec-fetch-dest": "empty",
159
+ "sec-fetch-mode": "cors",
160
+ "sec-fetch-site": "same-origin",
161
+ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
162
+ }
163
+ response = requests.get(url, headers=headers)
164
+
165
+ return json.loads(response.text)
166
+
167
+ def extract_huggingface_username(username):
168
+ data = get_creator(username)
169
+ links = data.get('result', {}).get('data', {}).get('json', {}).get('links', [])
170
+
171
+ for link in links:
172
+ url = link.get('url', '')
173
+ if url.startswith('https://www.huggingface.co/'):
174
+ return url.split('/')[-1]
175
+
176
+ return None
177
+
178
+
179
+
180
  def check_civit_link(profile: Optional[gr.OAuthProfile], url):
181
  info, _ = process_url(url, download_files=False)
182
+ hf_username = extract_huggingface_username(info['creator'])
183
+ if(not hf_username):
184
+ no_username_text = 'Oops, your CivitAI profile seems to not have information about your Hugging Face account. Please visit <a href="https://civitai.com/user/account">https://civitai.com/user/account</a> and include it there<br><img src="https://i.imgur.com/hCbo9uL.png" />'
185
+ return no_username_text, gr.update()
186
+ if(profile.name != hf_username):
187
+ unmatched_username_text = 'Oops, the Hugging Face account in your CivitAI profile seems to be different than the one your are using here. Please visit <a href="https://civitai.com/user/account">https://civitai.com/user/account</a> and update it there<br><img src="https://i.imgur.com/hCbo9uL.png" />'
188
+ return unmatched_username_text, gr.update()
189
+ else:
190
+ return '', gr.update(interactive=True)
 
 
 
 
 
 
191
 
 
 
 
 
 
 
 
 
 
 
 
192
  def swap_fill(profile: Optional[gr.OAuthProfile]):
193
  if profile is None:
194
  return gr.update(visible=True), gr.update(visible=False)
 
238
  )
239
  #is_author = gr.Checkbox(label="Are you the model author?", info="If you are not the author, a disclaimer with information about the author and the CivitAI source will be added", value=False)
240
  instructions = gr.HTML("")
241
+ submit_button_civit = gr.Button("Upload model to Hugging Face", interactive=False)
242
  output = gr.Textbox(label="Output progress")
243
 
244
  demo.load(fn=swap_fill, outputs=[disabled_area, enabled_area])
245
+ submit_source_civit.change(fn=check_civit_link, inputs=[submit_source_civit], outputs=[instructions, submit_button_civit])
246
  submit_button_civit.click(fn=upload_civit_to_hf, inputs=[submit_source_civit], outputs=[output])
247
 
248
  demo.queue()