taslim19
commited on
Commit
·
b13ebf4
1
Parent(s):
20ea113
added ai for eval and sh
Browse files- DragMusic/plugins/tools/dev.py +43 -9
- DragMusic/utils/gemini.py +20 -0
- requirements.txt +1 -0
DragMusic/plugins/tools/dev.py
CHANGED
@@ -14,6 +14,7 @@ from DragMusic import app
|
|
14 |
from config import OWNER_ID
|
15 |
from DragMusic.core.mongo import mongodb
|
16 |
from DragMusic import userbot # Ensure userbot is imported
|
|
|
17 |
|
18 |
async def aexec(code, client, message):
|
19 |
afkdb = mongodb.afk
|
@@ -59,17 +60,32 @@ async def executor(client: app, message: Message):
|
|
59 |
redirected_output = sys.stdout = StringIO()
|
60 |
redirected_error = sys.stderr = StringIO()
|
61 |
stdout, stderr, exc = None, None, None
|
62 |
-
|
|
|
63 |
try:
|
64 |
await aexec(cmd, client, message)
|
65 |
except Exception:
|
66 |
exc = traceback.format_exc()
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
stdout, stderr = redirected_output.getvalue(), redirected_error.getvalue()
|
69 |
sys.stdout, sys.stderr = old_stdout, old_stderr
|
70 |
|
71 |
evaluation = exc or stderr or stdout or "Success"
|
72 |
final_output = f"<b>⥤ ʀᴇsᴜʟᴛ :</b>\n<pre language='python'>{evaluation}</pre>"
|
|
|
|
|
|
|
|
|
73 |
|
74 |
t2 = time()
|
75 |
keyboard = InlineKeyboardMarkup(
|
@@ -89,17 +105,28 @@ async def executor(client: app, message: Message):
|
|
89 |
return await edit_or_reply(message, text="<b>ᴇxᴀᴍᴩʟᴇ :</b>\nsh git pull")
|
90 |
|
91 |
text = message.text.split(None, 1)[1]
|
92 |
-
shell = re.split(r""" (?=(?:[^'"]|'[^']*'
|
93 |
-
|
|
|
|
|
|
|
|
|
94 |
try:
|
95 |
-
t1 = time() # Ensure t1 is defined before usage
|
96 |
process = subprocess.Popen(shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
97 |
output = process.stdout.read().decode("utf-8").strip() or "None"
|
98 |
except Exception as err:
|
99 |
exc_type, exc_obj, exc_tb = sys.exc_info()
|
100 |
errors = traceback.format_exception(etype=exc_type, value=exc_obj, tb=exc_tb)
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
t2 = time()
|
104 |
keyboard = InlineKeyboardMarkup(
|
105 |
[
|
@@ -109,8 +136,15 @@ async def executor(client: app, message: Message):
|
|
109 |
]
|
110 |
]
|
111 |
)
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
await message.stop_propagation()
|
116 |
|
|
|
14 |
from config import OWNER_ID
|
15 |
from DragMusic.core.mongo import mongodb
|
16 |
from DragMusic import userbot # Ensure userbot is imported
|
17 |
+
from DragMusic.utils.gemini import get_gemini_fix
|
18 |
|
19 |
async def aexec(code, client, message):
|
20 |
afkdb = mongodb.afk
|
|
|
60 |
redirected_output = sys.stdout = StringIO()
|
61 |
redirected_error = sys.stderr = StringIO()
|
62 |
stdout, stderr, exc = None, None, None
|
63 |
+
gemini_suggestion = None
|
64 |
+
gemini_result = None
|
65 |
try:
|
66 |
await aexec(cmd, client, message)
|
67 |
except Exception:
|
68 |
exc = traceback.format_exc()
|
69 |
+
# Try Gemini fix
|
70 |
+
gemini_suggestion = await get_gemini_fix(exc, cmd, mode="python")
|
71 |
+
if gemini_suggestion and gemini_suggestion.strip() != cmd.strip():
|
72 |
+
# Try to run Gemini's suggestion
|
73 |
+
try:
|
74 |
+
redirected_output = sys.stdout = StringIO()
|
75 |
+
redirected_error = sys.stderr = StringIO()
|
76 |
+
await aexec(gemini_suggestion, client, message)
|
77 |
+
gemini_result = redirected_output.getvalue() or "Success"
|
78 |
+
except Exception as e2:
|
79 |
+
gemini_result = f"Gemini fix also failed: {traceback.format_exc()}"
|
80 |
stdout, stderr = redirected_output.getvalue(), redirected_error.getvalue()
|
81 |
sys.stdout, sys.stderr = old_stdout, old_stderr
|
82 |
|
83 |
evaluation = exc or stderr or stdout or "Success"
|
84 |
final_output = f"<b>⥤ ʀᴇsᴜʟᴛ :</b>\n<pre language='python'>{evaluation}</pre>"
|
85 |
+
if gemini_suggestion:
|
86 |
+
final_output += f"\n\n<b>Gemini Suggestion:</b>\n<pre language='python'>{gemini_suggestion}</pre>"
|
87 |
+
if gemini_result:
|
88 |
+
final_output += f"\n<b>Gemini Result:</b>\n<pre language='python'>{gemini_result}</pre>"
|
89 |
|
90 |
t2 = time()
|
91 |
keyboard = InlineKeyboardMarkup(
|
|
|
105 |
return await edit_or_reply(message, text="<b>ᴇxᴀᴍᴩʟᴇ :</b>\nsh git pull")
|
106 |
|
107 |
text = message.text.split(None, 1)[1]
|
108 |
+
shell = re.split(r""" (?=(?:[^'\"]|'[^']*'|\"[^\"]*\")*$)""", text)
|
109 |
+
t1 = time() # Ensure t1 is defined before usage
|
110 |
+
output = None
|
111 |
+
exc = None
|
112 |
+
gemini_suggestion = None
|
113 |
+
gemini_result = None
|
114 |
try:
|
|
|
115 |
process = subprocess.Popen(shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
116 |
output = process.stdout.read().decode("utf-8").strip() or "None"
|
117 |
except Exception as err:
|
118 |
exc_type, exc_obj, exc_tb = sys.exc_info()
|
119 |
errors = traceback.format_exception(etype=exc_type, value=exc_obj, tb=exc_tb)
|
120 |
+
exc = ''.join(errors)
|
121 |
+
# Try Gemini fix
|
122 |
+
gemini_suggestion = await get_gemini_fix(exc, text, mode="shell")
|
123 |
+
if gemini_suggestion and gemini_suggestion.strip() != text.strip():
|
124 |
+
shell2 = re.split(r""" (?=(?:[^'\"]|'[^']*'|\"[^\"]*\")*$)""", gemini_suggestion)
|
125 |
+
try:
|
126 |
+
process2 = subprocess.Popen(shell2, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
127 |
+
gemini_result = process2.stdout.read().decode("utf-8").strip() or "None"
|
128 |
+
except Exception as e2:
|
129 |
+
gemini_result = f"Gemini fix also failed: {traceback.format_exc()}"
|
130 |
t2 = time()
|
131 |
keyboard = InlineKeyboardMarkup(
|
132 |
[
|
|
|
136 |
]
|
137 |
]
|
138 |
)
|
139 |
+
if exc:
|
140 |
+
final_output = f"<b>ERROR :</b>\n<pre>{exc}</pre>"
|
141 |
+
if gemini_suggestion:
|
142 |
+
final_output += f"\n\n<b>Gemini Suggestion:</b>\n<pre>{gemini_suggestion}</pre>"
|
143 |
+
if gemini_result:
|
144 |
+
final_output += f"\n<b>Gemini Result:</b>\n<pre>{gemini_result}</pre>"
|
145 |
+
else:
|
146 |
+
final_output = f"<b>OUTPUT :</b>\n<pre>{output}</pre>"
|
147 |
+
await edit_or_reply(message, text=final_output, reply_markup=keyboard)
|
148 |
|
149 |
await message.stop_propagation()
|
150 |
|
DragMusic/utils/gemini.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
import os
|
3 |
+
|
4 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
5 |
+
|
6 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
7 |
+
|
8 |
+
async def get_gemini_fix(error_message, code, mode="python"):
|
9 |
+
prompt = (
|
10 |
+
f"I tried to run the following {mode} code/command and got this error.\n"
|
11 |
+
f"Code/Command:\n{code}\n\n"
|
12 |
+
f"Error:\n{error_message}\n\n"
|
13 |
+
"Please provide a corrected version of the code/command only. Do not explain, just output the fixed code/command."
|
14 |
+
)
|
15 |
+
try:
|
16 |
+
model = genai.GenerativeModel("gemini-pro")
|
17 |
+
response = model.generate_content(prompt)
|
18 |
+
return response.text.strip()
|
19 |
+
except Exception as e:
|
20 |
+
return None
|
requirements.txt
CHANGED
@@ -47,3 +47,4 @@ pillow
|
|
47 |
imagehash
|
48 |
# ffmpeg is installed at the system level, but if you want a Python wrapper, uncomment the next line:
|
49 |
# ffmpeg-python
|
|
|
|
47 |
imagehash
|
48 |
# ffmpeg is installed at the system level, but if you want a Python wrapper, uncomment the next line:
|
49 |
# ffmpeg-python
|
50 |
+
google-generativeai
|