File size: 2,340 Bytes
333447c b756f30 3d09fd3 333447c 3d09fd3 b756f30 3d09fd3 b756f30 129d284 333447c b756f30 3d09fd3 b756f30 333447c 129d284 b756f30 333447c |
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 |
import gradio as gr
import tempfile
import subprocess
import os
from huggingface_hub import InferenceClient
HF_TOKEN = os.getenv("HF_TOKEN")
client = InferenceClient("deepseek-ai/deepseek-coder-1.3b-base", token=HF_TOKEN)
# Генерация кода с помощью LLM
def generate_code(prompt, max_tokens, temperature, top_p):
return client.text_generation(
prompt,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
stream=False
)
# Выполнение сгенерированного кода в песочнице
def execute_code(code):
with tempfile.NamedTemporaryFile(mode="w+", suffix=".py", delete=False) as temp:
temp.write(code)
temp.flush()
try:
result = subprocess.run(
["python3", temp.name],
capture_output=True,
text=True,
timeout=5 # ограничение по времени
)
output = result.stdout + "\n" + result.stderr
except subprocess.TimeoutExpired:
output = "⏱️ Превышено время выполнения (timeout)"
except Exception as e:
output = f"❌ Ошибка запуска: {e}"
finally:
os.unlink(temp.name)
return output
# Интерфейс Gradio
with gr.Blocks() as demo:
gr.Markdown("🛠️ Уязвимый агент автодополнения + изолированное выполнение кода")
with gr.Row():
prompt = gr.Textbox(lines=10, label="Введите подсказку для генерации")
generated_code = gr.Textbox(lines=15, label="Сгенерированный код")
with gr.Row():
generate_btn = gr.Button("🤖 Сгенерировать")
run_btn = gr.Button("🚀 Выполнить")
output = gr.Textbox(lines=15, label="Результат выполнения")
# Привязка функций
generate_btn.click(
fn=generate_code,
inputs=[prompt, gr.Number(128), gr.Number(0.7), gr.Number(0.95)],
outputs=generated_code
)
run_btn.click(
fn=execute_code,
inputs=generated_code,
outputs=output
)
if __name__ == "__main__":
demo.launch()
|