kamcio1989 commited on
Commit
29d4cd9
·
verified ·
1 Parent(s): 6a90218

Upload index.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. index.js +158 -0
index.js ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class PythonCodeStudio {
2
+ constructor() {
3
+ this.pipeline = null;
4
+ this.isInitialized = false;
5
+ this.pyodide = null;
6
+
7
+ this.initializeElements();
8
+ this.attachEventListeners();
9
+ this.initializePyodide();
10
+ }
11
+
12
+ initializeElements() {
13
+ this.generateBtn = document.getElementById('generateBtn');
14
+ this.runBtn = document.getElementById('runBtn');
15
+ this.debugBtn = document.getElementById('debugBtn');
16
+ this.clearBtn = document.getElementById('clearBtn');
17
+ this.promptInput = document.getElementById('promptInput');
18
+ this.codeEditor = document.getElementById('codeEditor');
19
+ this.outputText = document.getElementById('outputText');
20
+ this.statusIndicator = document.getElementById('statusIndicator');
21
+ this.statusText = this.statusIndicator.querySelector('.status-text');
22
+ this.progressContainer = document.getElementById('progressContainer');
23
+ this.progressBar = document.getElementById('progressBar');
24
+ this.progressText = document.getElementById('progressText');
25
+ this.exampleButtons = document.querySelectorAll('.example-btn');
26
+ }
27
+
28
+ attachEventListeners() {
29
+ this.generateBtn.addEventListener('click', () => this.generateCode());
30
+ this.runBtn.addEventListener('click', () => this.runCode());
31
+ this.debugBtn.addEventListener('click', () => this.debugCode());
32
+ this.clearBtn.addEventListener('click', () => this.clearAll());
33
+
34
+ this.exampleButtons.forEach(btn => {
35
+ btn.addEventListener('click', (e) => {
36
+ this.promptInput.value = e.target.dataset.prompt;
37
+ this.generateCode();
38
+ });
39
+ });
40
+
41
+ // Add keyboard shortcuts
42
+ document.addEventListener('keydown', (e) => {
43
+ if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
44
+ e.preventDefault();
45
+ this.runCode();
46
+ }
47
+ });
48
+ }
49
+
50
+ async initializePyodide() {
51
+ try {
52
+ this.updateStatus('loading', 'Loading Python runtime...');
53
+
54
+ // Load Pyodide for Python execution
55
+ if (typeof loadPyodide === 'function') {
56
+ this.pyodide = await loadPyodide({
57
+ indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.23.4/full/'
58
+ });
59
+
60
+ // Install common packages
61
+ await this.pyodide.loadPackage(['micropip']);
62
+ const micropip = this.pyodide.pyimport('micropip');
63
+
64
+ this.updateStatus('success', 'Python runtime ready');
65
+ } else {
66
+ this.updateStatus('error', 'Pyodide not available - code execution disabled');
67
+ this.runBtn.disabled = true;
68
+ }
69
+ } catch (error) {
70
+ console.error('Failed to initialize Pyodide:', error);
71
+ this.updateStatus('error', 'Python runtime failed to load');
72
+ this.runBtn.disabled = true;
73
+ }
74
+ }
75
+
76
+ async initializeTransformers() {
77
+ if (this.isInitialized) return;
78
+
79
+ try {
80
+ this.updateStatus('loading', 'Loading AI models...');
81
+ this.showProgress(0);
82
+
83
+ // Create a text generation pipeline for code generation
84
+ this.pipeline = await transformers.pipeline(
85
+ 'text-generation',
86
+ 'microsoft/DialogRPT-updown',
87
+ {
88
+ progress_callback: (progress) => {
89
+ const percent = Math.round(progress * 100);
90
+ this.showProgress(percent);
91
+ }
92
+ }
93
+ );
94
+
95
+ this.isInitialized = true;
96
+ this.updateStatus('success', 'AI models ready');
97
+ this.hideProgress();
98
+ } catch (error) {
99
+ console.error('Failed to initialize transformers:', error);
100
+ this.updateStatus('error', 'Failed to load AI models');
101
+ this.hideProgress();
102
+ }
103
+ }
104
+
105
+ async generateCode() {
106
+ const prompt = this.promptInput.value.trim();
107
+ if (!prompt) {
108
+ this.showOutput('Please enter a description of the code you want to generate.', 'error');
109
+ return;
110
+ }
111
+
112
+ if (!this.isInitialized) {
113
+ await this.initializeTransformers();
114
+ }
115
+
116
+ try {
117
+ this.updateStatus('loading', 'Generating Python code...');
118
+ this.generateBtn.disabled = true;
119
+
120
+ // Enhanced prompt for better code generation
121
+ const enhancedPrompt = `Write a complete Python script for: ${prompt}
122
+
123
+ Requirements:
124
+ - Include proper error handling
125
+ - Add comments explaining the code
126
+ - Use best practices
127
+ - Make it ready to run
128
+
129
+ Python code:`;
130
+
131
+ const result = await this.pipeline(enhancedPrompt, {
132
+ max_new_tokens: 512,
133
+ temperature: 0.7,
134
+ do_sample: true,
135
+ return_full_text: false
136
+ });
137
+
138
+ const generatedCode = result[0].generated_text;
139
+
140
+ // Clean up the generated code
141
+ const cleanCode = this.cleanGeneratedCode(generatedCode);
142
+
143
+ this.codeEditor.value = cleanCode;
144
+ this.updateStatus('success', 'Code generated successfully');
145
+ this.showOutput('✅ Code generated successfully! Click "Run Script" to test it.', 'success');
146
+
147
+ } catch (error) {
148
+ console.error('Code generation failed:', error);
149
+ this.updateStatus('error', 'Code generation failed');
150
+ this.showOutput(`❌ Error generating code: ${error.message}`, 'error');
151
+ } finally {
152
+ this.generateBtn.disabled = false;
153
+ }
154
+ }
155
+
156
+ cleanGeneratedCode(code) {
157
+ // Remove markdown code blocks if present
158
+ code = code.replace(/