File size: 2,894 Bytes
eda20c5
87c8f8d
0eaa87b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eda20c5
87c8f8d
 
0eaa87b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87c8f8d
 
 
0eaa87b
87c8f8d
0eaa87b
 
87c8f8d
 
 
 
 
 
0eaa87b
 
87c8f8d
0eaa87b
87c8f8d
 
 
 
 
0eaa87b
87c8f8d
0eaa87b
 
 
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
 
 
 
 
 
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
87c8f8d
0eaa87b
87c8f8d
0eaa87b
 
 
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
 
 
 
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
87c8f8d
0eaa87b
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
 
 
 
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
87c8f8d
0eaa87b
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
87c8f8d
 
0eaa87b
87c8f8d
0eaa87b
 
 
 
87c8f8d
eda20c5
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import gradio as gr
import subprocess
import threading

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"

print("Loading model...")

tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)

model = AutoModelForCausalLM.from_pretrained(
    MODEL_NAME,
    torch_dtype="auto",
    device_map="cpu"
)

print("Model loaded")


def ask_ai(prompt):

    try:
        messages = [
            {
                "role": "user",
                "content": prompt
            }
        ]

        text = tokenizer.apply_chat_template(
            messages,
            tokenize=False,
            add_generation_prompt=True
        )

        inputs = tokenizer(
            text,
            return_tensors="pt"
        )

        output = model.generate(
            **inputs,
            max_new_tokens=512,
            temperature=0.7,
            do_sample=True
        )

        result = tokenizer.decode(
            output[0],
            skip_special_tokens=True
        )

        return result

    except Exception as e:
        return str(e)


def execute_code(code):

    try:

        proc = subprocess.run(
            ["python3", "-c", code],
            capture_output=True,
            text=True,
            timeout=30
        )

        if proc.returncode == 0:
            return proc.stdout or "Done"

        return proc.stderr

    except Exception as e:
        return str(e)


def ai_and_run(prompt):

    ai_response = ask_ai(
        prompt +
        "\nReturn only executable Python code."
    )

    result = execute_code(ai_response)

    return (
        "AI RESPONSE:\n\n"
        + ai_response
        + "\n\nOUTPUT:\n\n"
        + result
    )


with gr.Blocks(theme=gr.themes.Soft()) as demo:

    gr.Markdown("# Qwen AI + Sandbox")

    with gr.Tab("Chat"):

        prompt = gr.Textbox(
            label="Message",
            lines=4
        )

        output = gr.Textbox(
            label="Response",
            lines=15
        )

        btn = gr.Button("Send")

        btn.click(
            ask_ai,
            prompt,
            output
        )

    with gr.Tab("Python Sandbox"):

        code = gr.Textbox(
            label="Python Code",
            lines=12
        )

        result = gr.Textbox(
            label="Output",
            lines=12
        )

        run = gr.Button("Run")

        run.click(
            execute_code,
            code,
            result
        )

    with gr.Tab("AI Generate & Run"):

        p = gr.Textbox(
            label="Instruction",
            lines=4
        )

        r = gr.Textbox(
            label="Result",
            lines=20
        )

        b = gr.Button("Generate & Run")

        b.click(
            ai_and_run,
            p,
            r
        )

demo.launch(
    server_name="0.0.0.0",
    server_port=7860
)