File size: 6,078 Bytes
c336648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

function PonyHelper_send_to(where, text){
    textarea = gradioApp().querySelector('#PonyHelper_selected_text textarea')
    textarea.value = text
    updateInput(textarea)

    gradioApp().querySelector('#PonyHelper_send_to_'+where).click()

    where == 'txt2img' ? switch_to_txt2img() : switch_to_img2img()
}

function PonyHelper_send_to_txt2img(text){ PonyHelper_send_to('txt2img', text) }
function PonyHelper_send_to_img2img(text){ PonyHelper_send_to('img2img', text) }

function submit_PonyHelper(){
    var id = randomId()
    requestProgress(id, gradioApp().getElementById('PonyHelper_results_column'), null, function(){})

    var res = create_submit_args(arguments)
    res[0] = id
    return res
}
    // Function to format tags without category names and unnecessary symbols
    function formatTags(tagsResult) {
        var formattedTags = '';
        for (var key in tagsResult) {
            if (tagsResult.hasOwnProperty(key)) {
                formattedTags += tagsResult[key].replace(/"|:/g, '') + ', ';
            }
        }
        // Remove the trailing comma and space
        formattedTags = formattedTags.slice(0, -2);
        return formattedTags;
    }
		
$(document).ready(function(){
    $("#generate-tags-button").click(function(e){
    e.preventDefault();
    $.ajax({
        url: '/generate-tags/' + $('#category').val(),  // Include the selected category in the URL
        type: 'POST',
        data: $('form').serialize(),
        success: function(response){
            // Update the tags box with the generated tags
            $('.tags-box p').text(response.tags_result);
        },
        error: function(error){
            console.log(error);
        }
    });

        // Fetch updated CFG scale and sampling steps
        fetch('/generate-random-values')
            .then(response => response.json())
            .then(data => {
                // Update the CFG scale and sampling steps elements in the HTML
                $('#cfg-scale').text("CFG Scale: " + data.cfg);
                $('#sampling-steps').text("Sampling Steps: " + data.steps);
            })
            .catch(error => console.error('Error:', error));
    });
});
    // JavaScript function to handle the response and update the character tags box
    document.addEventListener('DOMContentLoaded', function() {
        const charactersForm = document.getElementById('characters-form');
        const charactersTagsBox = document.getElementById('characters-tags-box');
        const generateCharacterButton = document.getElementById('generate-character-button');

        generateCharacterButton.addEventListener('click', function() {
            fetch(charactersForm.getAttribute('action'), {
                method: 'POST',
                body: new FormData(charactersForm)
            })
            .then(response => response.json())
            .then(data => {
                charactersTagsBox.innerHTML = ''; // Clear existing content
                const p = document.createElement('p');
                p.textContent = data.characters_result; // Set text content to the generated character tags
                charactersTagsBox.appendChild(p); // Append the <p> element to the character tags box
            })
            .catch(error => console.error('Error:', error));
        });

        // Prevent default form submission
        charactersForm.addEventListener('submit', function(event) {
            event.preventDefault();
        });
    });

	// Function to handle form submission and display generated tags
function handleFormSubmit(form, resultElement) {
    form.addEventListener('submit', function(event) {
        event.preventDefault(); // Prevent default form submission

        // Send a POST request to the form's action URL
        fetch(form.action, {
            method: 'POST',
            body: new FormData(form)
        })
        .then(response => response.json())
        .then(data => {
            // Update the result element with the generated tags
            resultElement.textContent = data.result || data.characters_result;
        })
        .catch(error => console.error('Error:', error));
    });
}

  function handleButtonClick(type) {
    const generatedPrompt = "Your generated prompt goes here"; // Replace with actual prompt

    if (type === "img2img") {
      Ponyhelper_send_to_img2img(generatedPrompt);
    } else if (type === "txt2img") {
      Ponyhelper_send_to_txt2img(generatedPrompt);
    }
  }

  // Event listeners for button clicks
  document.getElementById("img2imgButton").addEventListener("click", () => {
    handleButtonClick("img2img");
  });

  document.getElementById("txt2imgButton").addEventListener("click", () => {
    handleButtonClick("txt2img");
  });
	
// Get the forms and result elements
const tagsForm = document.getElementById('tags-form');
const charactersForm = document.getElementById('characters-form');
const tagsResult = document.getElementById('tags-result');
const charactersResult = document.getElementById('characters-result');

// Handle form submission for generating DanBooru tags
handleFormSubmit(tagsForm, tagsResult);

// Handle form submission for generating character tags
handleFormSubmit(charactersForm, charactersResult);
		
        // Add JavaScript code to validate the form input
        document.addEventListener('DOMContentLoaded', function() {
            const form = document.querySelector('form');

            form.addEventListener('submit', function(event) {
                const numWordsInput = document.getElementById('num_words');
                const numWords = parseInt(numWordsInput.value);

                if (isNaN(numWords) || numWords < 1 || numWords > 50) {
                    alert('Please enter a valid number of DanBooru tags (1-50).');
                    event.preventDefault(); // Prevent form submission
                }
            });
        });
    });