File size: 8,285 Bytes
15c06cc
0c5a382
 
919ef54
e3dc1e5
3c68fa9
6e1a42f
 
 
919ef54
15c06cc
edee20e
6e1a42f
 
 
15c06cc
919ef54
6e1a42f
919ef54
 
7035fed
919ef54
6e1a42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5a382
6e1a42f
1149742
6e1a42f
 
 
1149742
6e1a42f
919ef54
6e1a42f
0c5a382
6e1a42f
919ef54
6e1a42f
1149742
0c5a382
 
 
6e1a42f
 
 
 
 
0c5a382
6e1a42f
1149742
6e1a42f
1149742
 
6e1a42f
919ef54
6e1a42f
0c5a382
6e1a42f
919ef54
6e1a42f
1149742
0c5a382
 
 
6e1a42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5a382
 
 
 
 
6e1a42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1149742
0c5a382
919ef54
6e1a42f
 
 
 
1149742
6e1a42f
1149742
6e1a42f
1149742
6e1a42f
1149742
6e1a42f
1149742
 
6e1a42f
1149742
6e1a42f
1149742
 
6e1a42f
0c5a382
1149742
0c5a382
6e1a42f
 
 
 
 
 
 
 
a6c0d93
0c5a382
6e1a42f
0c5a382
6e1a42f
 
 
 
 
 
 
8321f99
6e1a42f
0c5a382
6e1a42f
 
 
 
 
 
 
0c5a382
 
6e1a42f
0c5a382
6e1a42f
 
 
 
 
 
8321f99
6e1a42f
 
 
 
 
 
8321f99
6e1a42f
0c5a382
6e1a42f
0c5a382
6e1a42f
 
 
1149742
8321f99
0c5a382
 
6e1a42f
 
 
e3dc1e5
6e1a42f
 
 
 
 
 
e3dc1e5
 
6e1a42f
 
 
 
 
 
e3dc1e5
a6c0d93
6e1a42f
 
 
 
 
 
 
 
 
 
 
a6c0d93
 
 
6e1a42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c0d93
 
6e1a42f
 
 
 
 
 
 
 
 
 
a6c0d93
 
6e1a42f
 
 
a6c0d93
6e1a42f
 
 
 
 
 
 
 
a6c0d93
6e1a42f
 
 
 
 
 
 
0bb2b09
a6c0d93
6e1a42f
 
 
 
 
a6c0d93
6e1a42f
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import gradio as gr
import tempfile
import imageio
import torch
import time
import os

from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
from diffusers import DiffusionPipeline


# -------------------------------------------------
# Device Setup
# -------------------------------------------------

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

text_model_cache = {}
chat_memory = {}


# -------------------------------------------------
# Available Text Models
# -------------------------------------------------

AVAILABLE_MODELS = {
    "Codette LoRA (Llama-3.1)": "codette_lora",
    "Mistral-7B Instruct": "mistralai/Mistral-7B-Instruct-v0.2",
    "Phi-3 Mini": "microsoft/phi-3-mini-4k-instruct",
    "GPT-2 (lightweight)": "gpt2"
}


# -------------------------------------------------
# Load Codette LoRA Adapter
# -------------------------------------------------

def load_codette_lora():

    base_model = "meta-llama/Meta-Llama-3.1-8B"

    tokenizer = AutoTokenizer.from_pretrained(base_model)

    model = AutoModelForCausalLM.from_pretrained(
        base_model,
        torch_dtype=torch.float16 if device == "cuda" else torch.float32,
        device_map="auto"
    )

    model = PeftModel.from_pretrained(
        model,
        "Raiff1982/codette-lora-adapters"
    )

    pipe = pipeline(
        "text-generation",
        model=model,
        tokenizer=tokenizer,
        device_map="auto"
    )

    return pipe


# -------------------------------------------------
# Image Generator (SDXL Turbo)
# -------------------------------------------------

try:

    image_generator = DiffusionPipeline.from_pretrained(
        "stabilityai/sdxl-turbo",
        torch_dtype=torch.float16 if device == "cuda" else torch.float32,
        variant="fp16" if device == "cuda" else None
    )

    image_generator.to(device)

    image_enabled = True

except Exception as e:

    print(f"[Image Model Load Error]: {e}")
    image_generator = None
    image_enabled = False


# -------------------------------------------------
# Video Generator (Zeroscope)
# -------------------------------------------------

try:

    video_pipeline = DiffusionPipeline.from_pretrained(
        "cerspense/zeroscope_v2_576w",
        torch_dtype=torch.float16 if device == "cuda" else torch.float32
    )

    video_pipeline.to(device)

    video_enabled = True

except Exception as e:

    print(f"[Video Model Load Error]: {e}")
    video_pipeline = None
    video_enabled = False


# -------------------------------------------------
# Load Text Models
# -------------------------------------------------

def get_text_model(model_name):

    if model_name not in text_model_cache:

        if AVAILABLE_MODELS[model_name] == "codette_lora":

            text_model_cache[model_name] = load_codette_lora()

        else:

            text_model_cache[model_name] = pipeline(
                "text-generation",
                model=AVAILABLE_MODELS[model_name],
                device=0 if device == "cuda" else -1
            )

    return text_model_cache[model_name]


# -------------------------------------------------
# Codette Terminal Logic
# -------------------------------------------------

def codette_terminal(prompt, model_name, generate_image, generate_video,
                     session_id, batch_size, video_steps, fps):

    if session_id not in chat_memory:
        chat_memory[session_id] = []

    if prompt.lower() in ["exit", "quit"]:
        chat_memory[session_id] = []
        yield "🧠 Codette session reset.", None, None
        return

    try:

        model = get_text_model(model_name)

        result = model(
            prompt,
            max_new_tokens=200,
            temperature=0.7,
            top_p=0.9,
            do_sample=True
        )

        output = result[0]["generated_text"]

    except Exception as e:

        yield f"[Text generation error]: {e}", None, None
        return


    # -------------------------------------------------
    # Stream Text
    # -------------------------------------------------

    response_so_far = ""

    for char in output:

        response_so_far += char

        temp_log = chat_memory[session_id][:]

        temp_log.append(f"🖋️ You > {prompt}")
        temp_log.append(f"🧠 Codette > {response_so_far}")

        yield "\n".join(temp_log[-10:]), None, None

        time.sleep(0.01)


    chat_memory[session_id].append(f"🖋️ You > {prompt}")
    chat_memory[session_id].append(f"🧠 Codette > {output}")


    imgs = None
    vid = None


    # -------------------------------------------------
    # Image Generation
    # -------------------------------------------------

    if generate_image and image_enabled:

        try:

            result = image_generator(
                prompt,
                num_images_per_prompt=batch_size,
                num_inference_steps=2
            )

            imgs = result.images

        except Exception as e:

            print(f"[Image error]: {e}")


    # -------------------------------------------------
    # Video Generation
    # -------------------------------------------------

    if generate_video and video_enabled:

        try:

            result = video_pipeline(
                prompt,
                num_inference_steps=video_steps
            )

            frames = result.frames

            temp_video_path = tempfile.NamedTemporaryFile(
                suffix=".mp4",
                delete=False
            ).name

            imageio.mimsave(temp_video_path, frames, fps=fps)

            vid = temp_video_path

        except Exception as e:

            print(f"[Video error]: {e}")


    yield "\n".join(chat_memory[session_id][-10:]), imgs, vid


# -------------------------------------------------
# Gradio Interface
# -------------------------------------------------

with gr.Blocks(title="🧬 Codette Terminal") as demo:

    gr.Markdown("## 🧬 Codette Terminal")
    gr.Markdown("Chat with Codette, generate images, and create short videos.")

    session_id = gr.Textbox(value="default_session", visible=False)

    with gr.Row():

        model_dropdown = gr.Dropdown(
            choices=list(AVAILABLE_MODELS.keys()),
            value="Codette LoRA (Llama-3.1)",
            label="Language Model"
        )

    with gr.Row():

        generate_image_toggle = gr.Checkbox(
            label="Generate Image(s)",
            value=False,
            interactive=image_enabled
        )

        generate_video_toggle = gr.Checkbox(
            label="Generate Video",
            value=False,
            interactive=video_enabled
        )

    with gr.Row():

        batch_size_slider = gr.Slider(
            label="Number of Images",
            minimum=1,
            maximum=4,
            step=1,
            value=1
        )

        video_steps_slider = gr.Slider(
            label="Video Inference Steps",
            minimum=10,
            maximum=50,
            step=10,
            value=20
        )

        fps_slider = gr.Slider(
            label="Video FPS",
            minimum=4,
            maximum=24,
            step=2,
            value=8
        )

    user_input = gr.Textbox(
        label="Your Prompt",
        placeholder="A robot dreaming on Mars...",
        lines=1
    )

    output_text = gr.Textbox(
        label="Codette Output",
        lines=15,
        interactive=False
    )

    with gr.Row():

        output_image = gr.Gallery(
            label="Generated Images",
            columns=2
        )

        output_video = gr.Video(
            label="Generated Video"
        )


    user_input.submit(

        codette_terminal,

        inputs=[
            user_input,
            model_dropdown,
            generate_image_toggle,
            generate_video_toggle,
            session_id,
            batch_size_slider,
            video_steps_slider,
            fps_slider
        ],

        outputs=[
            output_text,
            output_image,
            output_video
        ]

    )


# -------------------------------------------------
# Launch
# -------------------------------------------------

if __name__ == "__main__":
    demo.launch()