Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,85 @@
|
|
| 1 |
-
import spaces
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import zipfile
|
| 5 |
-
import
|
| 6 |
from llama_cpp import Llama
|
| 7 |
-
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
| 8 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
| 9 |
-
from llama_cpp_agent.chat_history import BasicChatHistory
|
| 10 |
-
from llama_cpp_agent.chat_history.messages import Roles
|
| 11 |
-
from huggingface_hub import hf_hub_download
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
MODEL_PATH = f"models/{MODEL_FILENAME}"
|
| 16 |
|
| 17 |
-
hf_hub_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
def
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
| 23 |
llm = Llama(
|
| 24 |
model_path=MODEL_PATH,
|
| 25 |
-
flash_attn=True,
|
| 26 |
n_gpu_layers=81,
|
| 27 |
-
n_batch=1024,
|
| 28 |
n_ctx=8192,
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
provider = LlamaCppPythonProvider(llm)
|
| 31 |
agent = LlamaCppAgent(
|
| 32 |
provider,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
messages = BasicChatHistory()
|
| 39 |
-
messages.add_message({"role": Roles.user, "content": prompt})
|
| 40 |
-
|
| 41 |
-
stream = agent.get_chat_response(
|
| 42 |
-
prompt,
|
| 43 |
-
llm_sampling_settings=provider.get_provider_default_settings(),
|
| 44 |
-
chat_history=messages,
|
| 45 |
-
returns_streaming_generator=False,
|
| 46 |
-
print_output=True
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
zip_path = os.path.join(temp_dir, "generated_code.zip")
|
| 54 |
-
|
| 55 |
-
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 56 |
-
for filename, code in file_map.items():
|
| 57 |
-
full_path = os.path.join(temp_dir, filename)
|
| 58 |
-
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 59 |
-
with open(full_path, "w", encoding="utf-8") as f:
|
| 60 |
-
f.write(code)
|
| 61 |
-
zipf.write(full_path, arcname=filename)
|
| 62 |
-
|
| 63 |
-
return zip_path
|
| 64 |
-
|
| 65 |
-
def parse_code_blocks(text):
|
| 66 |
-
import re
|
| 67 |
-
file_blocks = {}
|
| 68 |
-
pattern = r"###\s*(.*?)\n```(?:[a-zA-Z0-9+]*\n)?(.*?)```"
|
| 69 |
-
matches = re.findall(pattern, text, re.DOTALL)
|
| 70 |
-
for filename, content in matches:
|
| 71 |
-
cleaned_filename = filename.strip()
|
| 72 |
-
cleaned_code = content.strip()
|
| 73 |
-
file_blocks[cleaned_filename] = cleaned_code
|
| 74 |
-
return file_blocks
|
| 75 |
-
|
| 76 |
-
def interface_fn(prompt):
|
| 77 |
-
try:
|
| 78 |
-
zip_path = generate_code_zip(prompt)
|
| 79 |
-
return zip_path
|
| 80 |
-
except Exception as e:
|
| 81 |
-
return f"Error: {e}"
|
| 82 |
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
-
gr.Markdown("
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import spaces
|
| 4 |
import zipfile
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
from llama_cpp import Llama
|
| 7 |
+
from llama_cpp_agent import LlamaCppAgent, Tool, ToolCall, MessagesFormatterType
|
| 8 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
MODEL_NAME = "neuraldaredevil-8b-abliterated-q4_k_m-imat.gguf"
|
| 11 |
+
MODEL_PATH = f"models/{MODEL_NAME}"
|
|
|
|
| 12 |
|
| 13 |
+
hf_hub_download(
|
| 14 |
+
repo_id="tHottie/NeuralDaredevil-8B-abliterated-Q4_K_M-GGUF",
|
| 15 |
+
filename=MODEL_NAME,
|
| 16 |
+
local_dir="./models"
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
# Define tools for the agent
|
| 20 |
+
def create_file(filename: str, content: str) -> str:
|
| 21 |
+
os.makedirs("generated", exist_ok=True)
|
| 22 |
+
filepath = os.path.join("generated", filename)
|
| 23 |
+
with open(filepath, "w", encoding="utf-8") as f:
|
| 24 |
+
f.write(content)
|
| 25 |
+
return f"Created {filename}"
|
| 26 |
+
|
| 27 |
+
def package_zip(output_name="code_package.zip"):
|
| 28 |
+
zip_path = os.path.join("generated", output_name)
|
| 29 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 30 |
+
for root, _, files in os.walk("generated"):
|
| 31 |
+
for file in files:
|
| 32 |
+
if file != output_name:
|
| 33 |
+
zipf.write(os.path.join(root, file), arcname=file)
|
| 34 |
+
return zip_path
|
| 35 |
+
|
| 36 |
+
# Wrap tools as LLM-callable
|
| 37 |
+
tools = [
|
| 38 |
+
Tool(
|
| 39 |
+
name="create_file",
|
| 40 |
+
description="Create a file from given filename and content.",
|
| 41 |
+
parameters={"filename": str, "content": str},
|
| 42 |
+
function=create_file
|
| 43 |
+
),
|
| 44 |
+
Tool(
|
| 45 |
+
name="package_zip",
|
| 46 |
+
description="Package all created files into a zip and return path.",
|
| 47 |
+
parameters={},
|
| 48 |
+
function=package_zip
|
| 49 |
+
)
|
| 50 |
+
]
|
| 51 |
|
| 52 |
+
@spaces.GPU(duration=120)
|
| 53 |
+
def respond(prompt):
|
| 54 |
llm = Llama(
|
| 55 |
model_path=MODEL_PATH,
|
|
|
|
| 56 |
n_gpu_layers=81,
|
|
|
|
| 57 |
n_ctx=8192,
|
| 58 |
+
n_batch=1024,
|
| 59 |
+
verbose=False
|
| 60 |
)
|
| 61 |
provider = LlamaCppPythonProvider(llm)
|
| 62 |
agent = LlamaCppAgent(
|
| 63 |
provider,
|
| 64 |
+
tools=tools,
|
| 65 |
+
system_prompt="You are an agent that receives instructions and uses tools like create_file and package_zip to generate and package code files into a downloadable zip.",
|
| 66 |
+
predefined_messages_formatter_type=MessagesFormatterType.GEMMA_2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
+
output = ""
|
| 70 |
+
for chunk in agent.run(prompt):
|
| 71 |
+
output += chunk
|
| 72 |
+
return output, package_zip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
+
gr.Markdown("""### Code Generator with GGUF Model
|
| 76 |
+
Paste your AI response below. The model will generate code files and return a downloadable zip.
|
| 77 |
+
""")
|
| 78 |
+
input_text = gr.Textbox(lines=20, label="AI Response or Prompt")
|
| 79 |
+
output_text = gr.Textbox(lines=10, label="Agent Response")
|
| 80 |
+
download_file = gr.File(label="Download Code Package (.zip)")
|
| 81 |
+
submit_btn = gr.Button("Generate and Package")
|
| 82 |
+
|
| 83 |
+
submit_btn.click(respond, inputs=[input_text], outputs=[output_text, download_file])
|
| 84 |
|
| 85 |
+
demo.launch()
|