SeifElden2342532 commited on
Commit
6fa23a4
·
verified ·
1 Parent(s): 900c4b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -2
app.py CHANGED
@@ -30,7 +30,46 @@ def optimize(code, category):
30
  if not code.strip():
31
  return "Please enter some Python code."
32
 
 
 
 
 
 
 
 
33
  messages = [
34
  {"role": "system", "content": SYSTEM_PROMPT},
35
- {"role": "user", "content": f"Original Code:\n
36
- http://googleusercontent.com/immersive_entry_chip/0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  if not code.strip():
31
  return "Please enter some Python code."
32
 
33
+ # Using triple quotes f""" here prevents the "unterminated string" error
34
+ user_content = f"""Original Code:
35
+ ```python
36
+ {code}
37
+ ```
38
+ Category: {category}"""
39
+
40
  messages = [
41
  {"role": "system", "content": SYSTEM_PROMPT},
42
+ {"role": "user", "content": user_content}
43
+ ]
44
+
45
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
46
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
47
+
48
+ with torch.no_grad():
49
+ generated_ids = model.generate(
50
+ **model_inputs,
51
+ max_new_tokens=1024,
52
+ do_sample=True,
53
+ temperature=0.7,
54
+ top_p=0.95,
55
+ pad_token_id=tokenizer.eos_token_id
56
+ )
57
+
58
+ input_len = model_inputs["input_ids"].shape[1]
59
+ output_ids = generated_ids[0][input_len:]
60
+ return tokenizer.decode(output_ids, skip_special_tokens=True)
61
+
62
+ demo = gr.Interface(
63
+ fn=optimize,
64
+ inputs=[
65
+ gr.Code(language="python", label="Your Python Code", lines=15),
66
+ gr.Radio(choices=["Performance", "Readability", "Conciseness"], value="Performance", label="Optimization Category")
67
+ ],
68
+ outputs=gr.Textbox(label="Optimized Code & Explanation", lines=20),
69
+ title="⚡ Python Code Optimizer",
70
+ description="QLoRA fine-tuned Qwen2.5-Coder-7B.",
71
+ flagging_mode="never"
72
+ )
73
+
74
+ if __name__ == "__main__":
75
+ demo.launch()