Spaces:
Sleeping
Sleeping
app.py and requirements.txt added
Browse files- app.py +46 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
MODEL_NAME = "tiiuae/falcon-180B"
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
MODEL_NAME,
|
10 |
+
torch_dtype=torch.bfloat16,
|
11 |
+
device_map="auto",
|
12 |
+
trust_remote_code=True
|
13 |
+
)
|
14 |
+
|
15 |
+
def improve_code(code: str) -> str:
|
16 |
+
prompt = (
|
17 |
+
"You are an expert code assistant.\n"
|
18 |
+
"Given the following code, suggest an improved version with clear comments and best practices.\n"
|
19 |
+
"Output only the improved code.\n\n"
|
20 |
+
f"Original code:\n{code}\n\nImproved code:"
|
21 |
+
)
|
22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
23 |
+
with torch.no_grad():
|
24 |
+
outputs = model.generate(
|
25 |
+
input_ids=inputs["input_ids"],
|
26 |
+
attention_mask=inputs["attention_mask"],
|
27 |
+
max_new_tokens=512,
|
28 |
+
temperature=0.2,
|
29 |
+
top_p=0.9,
|
30 |
+
do_sample=True,
|
31 |
+
pad_token_id=tokenizer.eos_token_id
|
32 |
+
)
|
33 |
+
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
+
improved = generated.split("Improved code:")[-1].strip()
|
35 |
+
return improved
|
36 |
+
|
37 |
+
app = gr.Blocks()
|
38 |
+
|
39 |
+
with app:
|
40 |
+
gr.Markdown("## Servidor MCP para mejora de c贸digo con Falcon 180B")
|
41 |
+
code_input = gr.Textbox(label="C贸digo original", lines=15)
|
42 |
+
improve_btn = gr.Button("Mejorar c贸digo")
|
43 |
+
code_output = gr.Textbox(label="C贸digo mejorado", lines=15)
|
44 |
+
improve_btn.click(improve_code, inputs=code_input, outputs=code_output)
|
45 |
+
|
46 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio[mcp]
|
2 |
+
transformers
|
3 |
+
torch
|