1234shaul commited on
Commit
8f9bccd
ยท
verified ยท
1 Parent(s): 676e9b8

requirements.txt

Browse files

gradio
google-generativeai

Files changed (1) hide show
  1. app.py +225 -0
app.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import subprocess
4
+ from pathlib import Path
5
+ from google.oauth2 import service_account
6
+ from googleapiclient.discovery import build
7
+ from googleapiclient.http import MediaFileUpload
8
+ import google.generativeai as genai
9
+ import re
10
+
11
+
12
+ # ๐Ÿ“‚ ื”ื’ื“ืจื•ืช
13
+ workspace_dir = Path("/home/user/app/app_project/workspace").resolve()
14
+ workspace_dir.mkdir(parents=True, exist_ok=True)
15
+
16
+
17
+ # ๐Ÿ› ๏ธ ืคืขื•ืœื•ืช ืงื‘ืฆื™ื
18
+
19
+
20
+ def list_files():
21
+ files = []
22
+ for file in workspace_dir.glob("*"):
23
+ if file.is_file():
24
+ files.append(file.name)
25
+ return files
26
+
27
+
28
+ def list_files_with_default():
29
+ files = list_files()
30
+ default = files[0] if files else None
31
+ return gr.Dropdown.update(choices=files, value=default)
32
+
33
+
34
+ def load_file(filename):
35
+ if not filename:
36
+ return ""
37
+ filepath = workspace_dir / filename
38
+ if not filepath.exists():
39
+ return ""
40
+ with open(filepath, "r", encoding="utf-8") as f:
41
+ return f.read()
42
+
43
+
44
+ def save_file(filename, content):
45
+ if not filename or filename.strip() == "":
46
+ return "No filename specified", list_files_with_default()
47
+ filepath = workspace_dir / filename
48
+ try:
49
+ with open(filepath, "w", encoding="utf-8") as f:
50
+ f.write(content)
51
+ return f"Saved {filename}", list_files_with_default()
52
+ except Exception as e:
53
+ return f"Error saving file: {e}", list_files_with_default()
54
+
55
+
56
+ def delete_file(filename):
57
+ if not filename or filename.strip() == "":
58
+ return "No filename specified", list_files_with_default()
59
+ filepath = workspace_dir / filename
60
+ if filepath.exists():
61
+ filepath.unlink()
62
+ return f"Deleted {filename}", list_files_with_default()
63
+ return "File not found", list_files_with_default()
64
+
65
+
66
+ def rename_file(filename, new_name):
67
+ if not filename or filename.strip() == "" or not new_name or new_name.strip() == "":
68
+ return "Filename or new name missing", list_files_with_default()
69
+ filepath = workspace_dir / filename
70
+ new_path = workspace_dir / new_name
71
+ if not filepath.exists():
72
+ return "File not found", list_files_with_default()
73
+ if new_path.exists():
74
+ return "File with new name already exists", list_files_with_default()
75
+ try:
76
+ filepath.rename(new_path)
77
+ return f"Renamed to {new_name}", list_files_with_default()
78
+ except Exception as e:
79
+ return f"Error renaming file: {e}", list_files_with_default()
80
+
81
+
82
+ def download_file(filename):
83
+ if not filename or filename.strip() == "":
84
+ return None
85
+ filepath = workspace_dir / filename
86
+ if not filepath.exists():
87
+ return None
88
+ return filepath
89
+
90
+
91
+ # โ˜๏ธ ื”ืขืœืื” ืœื“ืจื™ื™ื‘ (ื“ืจืš SECRET)
92
+ def upload_to_drive(filename):
93
+ if not filename or filename.strip() == "":
94
+ return "No filename specified"
95
+ creds = service_account.Credentials.from_service_account_file(
96
+ os.getenv("GOOGLE_SERVICE_ACCOUNT"),
97
+ scopes=['https://www.googleapis.com/auth/drive.file']
98
+ )
99
+ service = build('drive', 'v3', credentials=creds)
100
+ file_metadata = {'name': os.path.basename(filename)}
101
+ media = MediaFileUpload(str(workspace_dir / filename))
102
+ file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
103
+ return f"Uploaded to Drive. File ID: {file.get('id')}"
104
+
105
+
106
+ # ๐Ÿงฌ ืงื•ืคื™ื™ืœื•ื˜ Gemini (Flash)
107
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
108
+
109
+
110
+ def copilot_suggest(code, output, prompt):
111
+ full_prompt = f"""
112
+ You are a helpful coding assistant.
113
+
114
+ Here is the code:
115
+ {code}
116
+
117
+ Output:
118
+ {output}
119
+
120
+ Task:
121
+ {prompt}
122
+
123
+ Return ONLY in this format:
124
+ #### explanation here ####
125
+ $$$$
126
+ code block here
127
+ $$$$
128
+ """
129
+ model = genai.GenerativeModel('models/gemini-2.0-flash')
130
+ response = model.generate_content(full_prompt)
131
+ suggestion = response.text
132
+
133
+
134
+ explanation_match = re.search(r"####(.*?)####", suggestion, re.DOTALL)
135
+ code_match = re.search(r"\${4}(.*?)\${4}", suggestion, re.DOTALL)
136
+
137
+
138
+ explanation = explanation_match.group(1).strip() if explanation_match else "ืœื ื ืžืฆืื” ืชืฉื•ื‘ื”"
139
+ code = code_match.group(1).strip() if code_match else ""
140
+
141
+
142
+ return explanation, code
143
+
144
+
145
+ # โ–ถ๏ธ ื”ืจืฆืช ืงื•ื“ ืคื™ื™ืชื•ืŸ
146
+ def run_python(code):
147
+ tmp_filename = "temp_script.py"
148
+ filepath = workspace_dir / tmp_filename
149
+ try:
150
+ with open(filepath, "w", encoding="utf-8") as f:
151
+ f.write(code)
152
+ result = subprocess.run(
153
+ ["python3", str(filepath)],
154
+ capture_output=True,
155
+ text=True,
156
+ timeout=15
157
+ )
158
+ return result.stdout + result.stderr
159
+ except Exception as e:
160
+ return str(e)
161
+
162
+
163
+ # ๐Ÿ’ป ื”ืจืฆืช ื‘ืืฉ
164
+ def run_shell(command):
165
+ try:
166
+ result = subprocess.run(command, shell=True, capture_output=True, text=True)
167
+ return result.stdout + result.stderr
168
+ except Exception as e:
169
+ return str(e)
170
+
171
+
172
+ # ๐Ÿ–ผ๏ธ ืžืžืฉืง Gradio
173
+
174
+
175
+ with gr.Blocks(title="File Manager + Python Runner + Copilot") as app:
176
+ gr.Markdown("## ๐Ÿ“‚ Advanced File Manager + ๐Ÿ Python Runner + ๐Ÿงฌ Copilot")
177
+
178
+
179
+ with gr.Row():
180
+ with gr.Column(scale=1):
181
+ file_list = gr.Dropdown(choices=list_files(), label="๐Ÿ“‚ Files", interactive=True)
182
+ refresh_btn = gr.Button("๐Ÿ”„ Refresh Files")
183
+ refresh_msg = gr.Textbox(label="ืฉื’ื™ืื” / ืกื˜ื˜ื•ืก", interactive=False)
184
+
185
+
186
+ with gr.Row():
187
+ edit_btn = gr.Button("โœ๏ธ Edit")
188
+ delete_btn = gr.Button("๐Ÿ—‘๏ธ Delete")
189
+ download_btn = gr.Button("โฌ‡๏ธ Download")
190
+ rename_new_name = gr.Textbox(placeholder="New name...")
191
+ rename_btn = gr.Button("โœ๏ธ Rename")
192
+ upload_btn = gr.Button("โ˜๏ธ Upload to Drive")
193
+
194
+
195
+ with gr.Column(scale=3):
196
+ editor = gr.Code(label="๐Ÿ“ File Content", language="python")
197
+ filename_input = gr.Textbox(label="๐Ÿ“„ Filename (ืœืฉืžื™ืจื”/ื™ืฆื™ืจื”)", placeholder="ื”ื›ื ืก ืฉื ืงื•ื‘ืฅ ื›ืืŸ")
198
+ save_btn = gr.Button("๐Ÿ’พ Save File")
199
+ python_output = gr.Textbox(label="๐Ÿ Python Output")
200
+ run_py = gr.Button("โ–ถ๏ธ Run Python Code")
201
+ shell_command = gr.Textbox(label="๐Ÿ’ป Shell Command")
202
+ shell_output = gr.Textbox(label="๐Ÿ—…๏ธ Shell Output")
203
+ run_shell_btn = gr.Button("โ–ถ๏ธ Run Shell Command")
204
+ copilot_input = gr.Textbox(label="๐Ÿ’ฌ Copilot Prompt")
205
+ copilot_reply = gr.Textbox(label="๐Ÿงฌ Copilot Suggestion")
206
+ copilot_code = gr.Code(label="๐Ÿ“ Suggested Code", language="python")
207
+ copilot_btn = gr.Button("โœจ Ask Copilot")
208
+
209
+
210
+ # ืคืขื•ืœื•ืช
211
+ refresh_btn.click(fn=list_files_with_default, outputs=file_list)
212
+
213
+
214
+ edit_btn.click(fn=load_file, inputs=file_list, outputs=editor)
215
+ save_btn.click(fn=save_file, inputs=[filename_input, editor], outputs=[refresh_msg, file_list])
216
+ delete_btn.click(fn=delete_file, inputs=file_list, outputs=[refresh_msg, file_list])
217
+ download_btn.click(fn=download_file, inputs=file_list, outputs=gr.File())
218
+ rename_btn.click(fn=rename_file, inputs=[file_list, rename_new_name], outputs=[refresh_msg, file_list])
219
+ upload_btn.click(fn=upload_to_drive, inputs=file_list, outputs=refresh_msg)
220
+ run_py.click(fn=run_python, inputs=editor, outputs=python_output)
221
+ run_shell_btn.click(fn=run_shell, inputs=shell_command, outputs=shell_output)
222
+ copilot_btn.click(fn=copilot_suggest, inputs=[editor, python_output, copilot_input], outputs=[copilot_reply, copilot_code])
223
+
224
+
225
+ app.launch()