multimodalart HF staff commited on
Commit
8971c45
1 Parent(s): 012fe3d

Update share_btn.py

Browse files
Files changed (1) hide show
  1. share_btn.py +39 -24
share_btn.py CHANGED
@@ -9,20 +9,20 @@ loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" cl
9
  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
10
 
11
  share_js = """async () => {
12
- async function uploadFile(file){
13
- const UPLOAD_URL = 'https://huggingface.co/uploads';
14
- const response = await fetch(UPLOAD_URL, {
15
- method: 'POST',
16
- headers: {
17
- 'Content-Type': file.type,
18
- 'X-Requested-With': 'XMLHttpRequest',
19
- },
20
- body: file, /// <- File inherits from Blob
21
- });
22
- const url = await response.text();
23
- return url;
24
- }
25
-
26
  async function getInputImgFile(imgEl){
27
  const res = await fetch(imgEl.src);
28
  const blob = await res.blob();
@@ -35,12 +35,15 @@ share_js = """async () => {
35
  const fileName = `sd-perception-${{imgId}}.jpg`;
36
  return new File([blob], fileName, { type: 'image/jpeg' });
37
  }
38
- }
39
-
40
  const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
41
- const selectedLoRA = gradioEl.querySelector('#selected_lora').innerHTML;
42
- const inputPrompt = gradioEl.querySelector('#prompt input').value;
43
- const outputImgEl = gradioEl.querySelector('#result-image img');
 
 
 
44
 
45
  const shareBtnEl = gradioEl.querySelector('#share-btn');
46
  const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
@@ -51,22 +54,34 @@ share_js = """async () => {
51
  loadingIconEl.style.removeProperty('display');
52
 
53
  const inputFile = await getInputImgFile(outputImgEl);
54
- const urlInputImg = await uploadFile(inputFile);
 
 
 
 
 
 
55
 
56
  const descriptionMd = `
57
- ${selectedLoRA}
 
 
 
 
58
  ### Prompt
59
  ${inputPrompt}
 
60
  #### Generated Image:
61
  <img src="${urlInputImg}" />
62
  `;
 
63
  const params = new URLSearchParams({
64
- title: inputPrompt,
65
  description: descriptionMd,
66
  preview: true
67
  });
68
- const paramsStr = params.toString();
69
- window.open(`https://huggingface.co/spaces/multimodalart/LoraTheExplorer/discussions/new?${paramsStr}`, '_blank');
70
  shareBtnEl.style.removeProperty('pointer-events');
71
  shareIconEl.style.removeProperty('display');
72
  loadingIconEl.style.display = 'none';
 
9
  xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
10
 
11
  share_js = """async () => {
12
+ async function uploadFile(file){
13
+ const UPLOAD_URL = 'https://huggingface.co/uploads';
14
+ const response = await fetch(UPLOAD_URL, {
15
+ method: 'POST',
16
+ headers: {
17
+ 'Content-Type': file.type,
18
+ 'X-Requested-With': 'XMLHttpRequest',
19
+ },
20
+ body: file, /// <- File inherits from Blob
21
+ });
22
+ const url = await response.text();
23
+ return url;
24
+ }
25
+
26
  async function getInputImgFile(imgEl){
27
  const res = await fetch(imgEl.src);
28
  const blob = await res.blob();
 
35
  const fileName = `sd-perception-${{imgId}}.jpg`;
36
  return new File([blob], fileName, { type: 'image/jpeg' });
37
  }
38
+ }
39
+ console.log("test")
40
  const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
41
+ const randomLoRA_1 = gradioEl.querySelector('#component-7 img');
42
+ const randomLoRA_1_title = gradioEl.querySelector('#component-7 [data-testid="block-label"] span').innerHTML
43
+ const randomLoRA_2 = gradioEl.querySelector('#component-7 img')
44
+ const randomLoRA_2_title = gradioEl.querySelector('component-7 [data-testid="block-label"] span').innerHTML
45
+ const prompt = gradioEl.querySelector('#prompt textarea').value
46
+ const outputImgEl = gradioEl.querySelector('#component-22 img');
47
 
48
  const shareBtnEl = gradioEl.querySelector('#share-btn');
49
  const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
 
54
  loadingIconEl.style.removeProperty('display');
55
 
56
  const inputFile = await getInputImgFile(outputImgEl);
57
+ const urlOutputImg = await uploadFile(inputFile);
58
+
59
+ const inputFile = await getInputImgFile(randomLoRA_1);
60
+ const urlLoRA1 = await uploadFile(inputFile);
61
+
62
+ const inputFile = await getInputImgFile(randomLoRA_2);
63
+ const urlLoRA2 = await uploadFile(inputFile);
64
 
65
  const descriptionMd = `
66
+
67
+ | {randomLoRA_1_title} | | {randomLoRA_2_title}|
68
+ |---------|-----------|---------|
69
+ | ![{randomLoRA_1_title}]({urlLoRA1}) | + | ![{randomLoRA_1_title}]({urlLoRA2}) |
70
+
71
  ### Prompt
72
  ${inputPrompt}
73
+
74
  #### Generated Image:
75
  <img src="${urlInputImg}" />
76
  `;
77
+ console.log(descriptionMd)
78
  const params = new URLSearchParams({
79
+ title: prompt,
80
  description: descriptionMd,
81
  preview: true
82
  });
83
+ const paramsStr = params.toString();
84
+ window.open(`https://huggingface.co/spaces/multimodalart/lora-roulette/discussions/new?${paramsStr}`, '_blank');
85
  shareBtnEl.style.removeProperty('pointer-events');
86
  shareIconEl.style.removeProperty('display');
87
  loadingIconEl.style.display = 'none';