File size: 3,781 Bytes
ca49620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
import torch

# ==========================
#  Load your model
# ==========================

MODEL_ID = "OSS-forge/DeepSeek-Coder-1.3B-cleaned"

device = "cuda" if torch.cuda.is_available() else "cpu"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
    device_map="auto" if torch.cuda.is_available() else None,
)
model.to(device)
model.eval()


# ==========================
#  Prompt builder
# ==========================

def build_instruction_prompt(instruction: str) -> str:
    return '''
You are an AI programming assistant, utilizing the DeepSeek Coder model, developed by DeepSeek Company, and you only answer questions related to computer science.
### Instruction:
{}
### Response:
'''.format(instruction.strip()).lstrip()


# ==========================
#  Gradio logic
# ==========================

def generate_code(instruction, chat_history, is_first_time):
    if chat_history is None or is_first_time:
        chat_history = []

    instruction = instruction.strip()
    if not instruction:
        return chat_history, gr.update(value=instruction), False

    prompt = build_instruction_prompt(instruction)

    inputs = tokenizer(
        prompt,
        return_tensors="pt",
        padding=True,
        truncation=True,
        max_length=512,
    ).to(device)

    try:
        stop_id = tokenizer.convert_tokens_to_ids("<|EOT|>")
    except Exception:
        stop_id = tokenizer.eos_token_id

    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=512,
            do_sample=False,
            pad_token_id=stop_id,
            eos_token_id=stop_id,
        )

    input_len = inputs["input_ids"].shape[1]
    generated_tokens = outputs[0, input_len:]
    code = tokenizer.decode(generated_tokens, skip_special_tokens=True).strip()

    user_message = f"**Instruction**:\n{instruction}"
    ai_message = f"**Generated code**:\n```python\n{code}\n```"

    chat_history = chat_history + [
        {"role": "user", "content": user_message},
        {"role": "assistant", "content": ai_message},
    ]

    return chat_history, gr.update(value=""), False


def reset_interface():
    return [], gr.update(value=""), True


# ==========================
#  Gradio UI
# ==========================

with gr.Blocks(title="Python Code Generator") as demo:
    gr.Markdown("# 🧠 Python Code Generator")
    gr.Markdown(
        "Generate Python code from natural language instructions using your Hugging Face model."
    )

    with gr.Row():
        with gr.Column(scale=2):
            instruction_input = gr.Textbox(
                label="Instruction",
                placeholder="Describe the code you want. E.g., 'Write a Python function that checks if a number is prime.'",
                lines=4,
            )

            is_first = gr.State(True)

            submit_btn = gr.Button("Generate Code")
            reset_btn = gr.Button("Start Over")

        with gr.Column(scale=3):
            chat_output = gr.Chatbot(
                label="Conversation",
                height=500,
            )

    submit_btn.click(
        fn=generate_code,
        inputs=[instruction_input, chat_output, is_first],
        outputs=[chat_output, instruction_input, is_first],
    )

    reset_btn.click(
        fn=reset_interface,
        outputs=[chat_output, instruction_input, is_first],
    )

if __name__ == "__main__":
    print("Launching Gradio interface...")
    demo.queue(max_size=10).launch()