Spaces:
Runtime error
Runtime error
Update requirements.txt
Browse files- requirements.txt +60 -15
requirements.txt
CHANGED
|
@@ -1,20 +1,65 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
accelerate>=0.26.0
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
+
# --- Model Setup ---
|
| 7 |
+
model_path = "WhiteRabbitNeo/WhiteRabbitNeo-2.5-Qwen-2.5-Coder-7B"
|
| 8 |
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_path,
|
| 11 |
+
torch_dtype=torch.float16,
|
| 12 |
+
device_map="auto", # requires accelerate
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
|
|
|
| 17 |
|
| 18 |
+
# --- Backend function ---
|
| 19 |
+
def generate_code(user_prompt, temperature=0.75, top_p=1.0, max_tokens=2048, top_k=50):
|
| 20 |
+
tokens = tokenizer.encode(user_prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
output = model.generate(
|
| 23 |
+
input_ids=tokens,
|
| 24 |
+
max_length=tokens.shape[1] + max_tokens,
|
| 25 |
+
do_sample=True,
|
| 26 |
+
temperature=temperature,
|
| 27 |
+
top_p=top_p,
|
| 28 |
+
top_k=top_k,
|
| 29 |
+
num_return_sequences=1,
|
| 30 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 31 |
+
)
|
| 32 |
+
generated_tokens = output[0][tokens.shape[1]:]
|
| 33 |
+
code_string = tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 34 |
+
return f"```python\n{code_string}\n```"
|
| 35 |
|
| 36 |
+
# --- FastAPI backend ---
|
| 37 |
+
api = FastAPI()
|
| 38 |
+
|
| 39 |
+
@api.get("/ping")
|
| 40 |
+
async def ping():
|
| 41 |
+
return {"status": "pong"}
|
| 42 |
+
|
| 43 |
+
# --- Gradio UI ---
|
| 44 |
+
with gr.Blocks(title="Spec Kit Copilot") as demo:
|
| 45 |
+
with gr.Tab("AI Code Generation"):
|
| 46 |
+
gr.Markdown("## WhiteRabbitNeo AI Code Generator")
|
| 47 |
+
user_input = gr.Textbox(label="Describe code to generate", lines=4, placeholder="E.g., Python function to sort a list")
|
| 48 |
+
temperature = gr.Slider(0.0, 1.0, 0.75, label="Temperature")
|
| 49 |
+
top_p = gr.Slider(0.0, 1.0, 1.0, label="Top-p")
|
| 50 |
+
max_tokens = gr.Slider(256, 4096, 2048, step=128, label="Max Tokens")
|
| 51 |
+
top_k = gr.Slider(0, 100, 50, label="Top-k")
|
| 52 |
+
generate_btn = gr.Button("Generate Code")
|
| 53 |
+
preview = gr.Markdown()
|
| 54 |
+
|
| 55 |
+
generate_btn.click(
|
| 56 |
+
fn=generate_code,
|
| 57 |
+
inputs=[user_input, temperature, top_p, max_tokens, top_k],
|
| 58 |
+
outputs=preview
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# --- Mount FastAPI inside Gradio (optional) ---
|
| 62 |
+
demo = gr.mount_app(demo, api, path="/api")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|