File size: 10,386 Bytes
3966f89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import os
import glob
import time
import pandas as pd
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from diffusers import StableDiffusionPipeline
import fitz
import requests
from PIL import Image
import logging
import asyncio
import aiofiles
from io import BytesIO
from dataclasses import dataclass
from typing import Optional
import gradio as gr

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
log_records = []

class LogCaptureHandler(logging.Handler):
    def emit(self, record):
        log_records.append(record)

logger.addHandler(LogCaptureHandler())

@dataclass
class ModelConfig:
    name: str
    base_model: str
    size: str
    domain: Optional[str] = None
    model_type: str = "causal_lm"
    @property
    def model_path(self):
        return f"models/{self.name}"

@dataclass
class DiffusionConfig:
    name: str
    base_model: str
    size: str
    domain: Optional[str] = None
    @property
    def model_path(self):
        return f"diffusion_models/{self.name}"

class ModelBuilder:
    def __init__(self):
        self.config = None
        self.model = None
        self.tokenizer = None
    def load_model(self, model_path: str, config: Optional[ModelConfig] = None):
        self.model = AutoModelForCausalLM.from_pretrained(model_path)
        self.tokenizer = AutoTokenizer.from_pretrained(model_path)
        if self.tokenizer.pad_token is None:
            self.tokenizer.pad_token = self.tokenizer.eos_token
        if config:
            self.config = config
        self.model.to("cuda" if torch.cuda.is_available() else "cpu")
        return self
    def save_model(self, path: str):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        self.model.save_pretrained(path)
        self.tokenizer.save_pretrained(path)

class DiffusionBuilder:
    def __init__(self):
        self.config = None
        self.pipeline = None
    def load_model(self, model_path: str, config: Optional[DiffusionConfig] = None):
        self.pipeline = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float32).to("cpu")
        if config:
            self.config = config
        return self
    def save_model(self, path: str):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        self.pipeline.save_pretrained(path)
    def generate(self, prompt: str):
        return self.pipeline(prompt, num_inference_steps=20).images[0]

def generate_filename(sequence, ext="png"):
    timestamp = time.strftime("%d%m%Y%H%M%S")
    return f"{sequence}_{timestamp}.{ext}"

def get_gallery_files(file_types):
    return sorted(list(set([f for ext in file_types for f in glob.glob(f"*.{ext}")])))  # Deduplicate files

async def process_image_gen(prompt, output_file, builder):
    if builder and isinstance(builder, DiffusionBuilder) and builder.pipeline:
        pipeline = builder.pipeline
    else:
        pipeline = StableDiffusionPipeline.from_pretrained("OFA-Sys/small-stable-diffusion-v0", torch_dtype=torch.float32).to("cpu")
    gen_image = pipeline(prompt, num_inference_steps=20).images[0]
    gen_image.save(output_file)
    return gen_image

# Smart Uploader Functions
def upload_files(files, links_title, links_url, history, selected_files):
    uploaded = {"images": [], "videos": [], "documents": [], "datasets": [], "links": []}
    if files:
        for file in files:
            ext = file.name.split('.')[-1].lower()
            output_path = f"uploaded_{int(time.time())}_{file.name}"
            with open(output_path, "wb") as f:
                f.write(file.read())
            if ext in ["jpg", "png"]:
                uploaded["images"].append(output_path)
            elif ext == "mp4":
                uploaded["videos"].append(output_path)
            elif ext in ["md", "pdf", "docx"]:
                uploaded["documents"].append(output_path)
            elif ext in ["csv", "xlsx"]:
                uploaded["datasets"].append(output_path)
            history.append(f"Uploaded: {output_path}")
            selected_files[output_path] = False  # Default unchecked
    if links_title and links_url:
        links = list(zip(links_title.split('\n'), links_url.split('\n')))
        for title, url in links:
            if title and url:
                link_entry = f"[{title}]({url})"
                uploaded["links"].append(link_entry)
                history.append(f"Added Link: {link_entry}")
                selected_files[link_entry] = False
    return uploaded, history, selected_files

def update_galleries(history, selected_files):
    galleries = {
        "images": get_gallery_files(["jpg", "png"]),
        "videos": get_gallery_files(["mp4"]),
        "documents": get_gallery_files(["md", "pdf", "docx"]),
        "datasets": get_gallery_files(["csv", "xlsx"]),
        "links": [f for f in selected_files.keys() if f.startswith('[') and '](' in f and f.endswith(')')]
    }
    gallery_outputs = {
        "images": [(Image.open(f), os.path.basename(f)) for f in galleries["images"][:4]],
        "videos": [(f, os.path.basename(f)) for f in galleries["videos"][:4]],  # Video preview as file path
        "documents": [(Image.frombytes("RGB", fitz.open(f)[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5)).size, fitz.open(f)[0].get_pixmap(matrix=fitz.Matrix(0.5, 0.5)).samples) if f.endswith('.pdf') else f, os.path.basename(f)) for f in galleries["documents"][:4]],
        "datasets": [(f, os.path.basename(f)) for f in galleries["datasets"][:4]],  # Text preview
        "links": [(f, f.split(']')[0][1:]) for f in galleries["links"][:4]]
    }
    history.append(f"Updated galleries: {sum(len(g) for g in galleries.values())} files")
    return gallery_outputs, history, selected_files

def toggle_selection(file_list, selected_files):
    for file in file_list:
        selected_files[file] = not selected_files.get(file, False)
    return selected_files

def image_gen(prompt, builder, history, selected_files):
    selected = [f for f, sel in selected_files.items() if sel and f.endswith(('.jpg', '.png'))]
    if not selected:
        return "No images selected", None, history, selected_files
    output_file = generate_filename("gen_output", "png")
    gen_image = asyncio.run(process_image_gen(prompt, output_file, builder))
    history.append(f"Image Gen: {prompt} -> {output_file}")
    selected_files[output_file] = True
    return f"Image saved to {output_file}", gen_image, history, selected_files

# Gradio UI
with gr.Blocks(title="AI Vision & SFT Titans πŸš€") as demo:
    gr.Markdown("# AI Vision & SFT Titans πŸš€")
    history = gr.State(value=[])
    builder = gr.State(value=None)
    selected_files = gr.State(value={})

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("## πŸ“ File Tree")
            with gr.Accordion("🌳 Uploads", open=True):
                with gr.Row():
                    gr.Markdown("### πŸ–ΌοΈ Images (jpg/png)")
                    img_gallery = gr.Gallery(label="Images", columns=4, height="auto")
                with gr.Row():
                    gr.Markdown("### πŸŽ₯ Videos (mp4)")
                    vid_gallery = gr.Gallery(label="Videos", columns=4, height="auto")
                with gr.Row():
                    gr.Markdown("### πŸ“œ Docs (md/pdf/docx)")
                    doc_gallery = gr.Gallery(label="Documents", columns=4, height="auto")
                with gr.Row():
                    gr.Markdown("### πŸ“Š Data (csv/xlsx)")
                    data_gallery = gr.Gallery(label="Datasets", columns=4, height="auto")
                with gr.Row():
                    gr.Markdown("### πŸ”— Links")
                    link_gallery = gr.Gallery(label="Links", columns=4, height="auto")
            gr.Markdown("## πŸ“œ History")
            history_output = gr.Textbox(label="Log", lines=5, interactive=False)

        with gr.Column(scale=3):
            with gr.Row():
                gr.Markdown("## πŸ› οΈ Toolbar")
                upload_btn = gr.Button("πŸ“€ Upload")
                select_btn = gr.Button("βœ… Select")
                gen_btn = gr.Button("🎨 Generate")

            with gr.Tabs():
                with gr.TabItem("πŸ“€ Smart Upload"):
                    file_upload = gr.File(label="Upload Files", file_count="multiple", type="binary")
                    links_title = gr.Textbox(label="Link Titles (one per line)", lines=3)
                    links_url = gr.Textbox(label="Link URLs (one per line)", lines=3)
                    upload_status = gr.Textbox(label="Status")

                with gr.TabItem("πŸ” Operations"):
                    prompt = gr.Textbox(label="Image Gen Prompt", value="Generate a neon version")
                    op_status = gr.Textbox(label="Status")
                    op_output = gr.Image(label="Output")

            upload_btn.click(
                upload_files,
                inputs=[file_upload, links_title, links_url, history, selected_files],
                outputs=[upload_status, history, selected_files]
            ).then(
                update_galleries,
                inputs=[history, selected_files],
                outputs=[img_gallery, vid_gallery, doc_gallery, data_gallery, link_gallery, history, selected_files]
            )

            select_btn.click(
                toggle_selection,
                inputs=[gr.Dropdown(choices=list(selected_files.value.keys()), multiselect=True, label="Select Files"), selected_files],
                outputs=[selected_files]
            ).then(
                update_galleries,
                inputs=[history, selected_files],
                outputs=[img_gallery, vid_gallery, doc_gallery, data_gallery, link_gallery, history, selected_files]
            )

            gen_btn.click(
                image_gen,
                inputs=[prompt, builder, history, selected_files],
                outputs=[op_status, op_output, history, selected_files]
            ).then(
                update_galleries,
                inputs=[history, selected_files],
                outputs=[img_gallery, vid_gallery, doc_gallery, data_gallery, link_gallery, history, selected_files]
            )

    # Update history output
    demo.load(lambda h: "\n".join(h[-5:]), inputs=[history], outputs=[history_output])

demo.launch()