raghualgt/python-coding-assistant
0
1import gradio as gr2from transformers import pipeline3 4generator = pipeline(5 "text-generation",6 model="deepseek-ai/deepseek-coder-1.3b-instruct",7 device_map="auto".8 clean_up_tokenization_spaces=True9)10 11SYSTEM_PROMPT = """12You are a senior Python engineer.13 14When answering:15- Return clean Python code16- Follow best practices17- Keep functions simple18- Add comments if useful19- Avoid unnecessary explanations20"""21 22def chat(message, history):23 24 prompt = SYSTEM_PROMPT + "\nUser: " + message + "\nAssistant:"25 26 result = generator(27 prompt,28 max_new_tokens=200,29 temperature=0.230 )31 32 output = result[0]["generated_text"]33 34 # Clean tokenizer artifacts35 output = output.replace("Ċ", "\n").replace("Ġ", " ")36 37 code = output.split("Assistant:")[-1]38 39 return f"```python\n{code}\n```"40 41 42demo = gr.ChatInterface(43 fn=chat,44 title="Python Coding Assistant",45 description="Ask Python coding questions"46)47 48demo.launch()