Sanshruth commited on
Commit
aff5a3a
1 Parent(s): 3b1994e

initial_commit

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import time
4
+ import gradio as gr
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ from tld.diffusion import DiffusionTransformer
7
+ from tld.configs import LTDConfig, DenoiserConfig, DenoiserLoad
8
+ import numpy as np
9
+ from PIL import Image
10
+
11
+ # Image Generation Model Setup
12
+ denoiser_cfg = DenoiserConfig(
13
+ image_size=32,
14
+ noise_embed_dims=256,
15
+ patch_size=2,
16
+ embed_dim=768,
17
+ dropout=0,
18
+ n_layers=12,
19
+ text_emb_size=768
20
+ )
21
+
22
+ denoiser_load = DenoiserLoad(**{
23
+ 'dtype': torch.float32,
24
+ 'file_url': 'https://huggingface.co/apapiu/small_ldt/resolve/main/state_dict_378000.pth',
25
+ 'local_filename': 'state_dict_378000.pth'
26
+ })
27
+
28
+ cfg = LTDConfig(denoiser_cfg=denoiser_cfg, denoiser_load=denoiser_load)
29
+ diffusion_transformer = DiffusionTransformer(cfg)
30
+
31
+ # Set PyTorch to use all available CPU cores
32
+ num_cores = os.cpu_count()
33
+ torch.set_num_threads(num_cores)
34
+ print(f"Using {num_cores} CPU cores.")
35
+
36
+ # Text Model Setup
37
+ model_name = 'mllmTeam/PhoneLM-1.5B-Instruct'
38
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map='cpu', trust_remote_code=True)
39
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
40
+
41
+ def generate_text_response(question):
42
+ start_time = time.time()
43
+ prompt = [{"role": "user", "content": question}]
44
+ input_text = tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)
45
+ inp = tokenizer(input_text, return_tensors="pt")
46
+ inp = {k: v.to('cpu') for k, v in inp.items()}
47
+ out = model.generate(**inp, max_length=256, do_sample=True, temperature=0.7, top_p=0.7)
48
+ text = tokenizer.decode(out[0], skip_special_tokens=True)
49
+ text = text.split("\n")[-1]
50
+ end_time = time.time()
51
+ elapsed_time = end_time - start_time
52
+ return text
53
+
54
+ def generate_image(prompt, class_guidance=6, num_imgs=1, seed=11):
55
+ start_time = time.time()
56
+ try:
57
+ # Generate the image
58
+ out = diffusion_transformer.generate_image_from_text(
59
+ prompt=prompt,
60
+ class_guidance=class_guidance,
61
+ num_imgs=num_imgs,
62
+ seed=seed
63
+ )
64
+
65
+ # Convert to PIL Image if it's not already
66
+ if isinstance(out, torch.Tensor):
67
+ out = out.squeeze().permute(1, 2, 0).numpy()
68
+
69
+ # Ensure the image is in the right format for Gradio
70
+ if isinstance(out, np.ndarray):
71
+ # Normalize pixel values to 0-255 range
72
+ out = ((out - out.min()) * (1/(out.max() - out.min()) * 255)).astype('uint8')
73
+ out = Image.fromarray(out)
74
+
75
+ end_time = time.time()
76
+ print(f"Image generation time: {end_time - start_time:.2f} seconds")
77
+ return out
78
+ except Exception as e:
79
+ print(f"Image generation error: {e}")
80
+ return None
81
+
82
+ def chat_with_ai(message, history):
83
+ max_history_length = 1 # Adjust as needed
84
+ history = history[-max_history_length:]
85
+ if message.startswith('@imagine'):
86
+ # Extract prompt after '@imagine'
87
+ image_prompt = message.split('@imagine', 1)[1].strip()
88
+ image = generate_image(image_prompt)
89
+
90
+ if image:
91
+ return "", history, image
92
+ else:
93
+ return "", history + [[message, "Failed to generate image."]], None
94
+ else:
95
+ response = generate_text_response(message)
96
+ return response, history + [[message, response]], None
97
+
98
+
99
+
100
+ # Create Gradio interface
101
+ with gr.Blocks(title="BlazeChat Image Generator") as demo:
102
+ #################
103
+ gr.Markdown("# ⚡Fast CPU-Powered Chat & Image Generation")
104
+ gr.Markdown("Generate text and images using advanced AI models on CPU. Use `@imagine [prompt]` to create images or chat naturally.")
105
+ gr.Markdown("https://github.com/SanshruthR/CPU_BlazeChat")
106
+ ####################
107
+ chatbot = gr.Chatbot()
108
+ msg = gr.Textbox(label="Enter your message")
109
+ ####submit button
110
+ submit_button = gr.Button("Submit")
111
+ ##########
112
+ clear = gr.Button("Clear")
113
+ img_output = gr.Image(label="Generated Image")
114
+
115
+ msg.submit(chat_with_ai, [msg, chatbot], [msg, chatbot, img_output])
116
+
117
+ ####################binding with submit
118
+ submit_button.click(chat_with_ai, [msg, chatbot], [msg, chatbot, img_output])
119
+
120
+
121
+
122
+ ###################
123
+ clear.click(lambda: None, None, chatbot, queue=False)
124
+
125
+ # Launch the demo
126
+ demo.launch(debug=True,ssr_mode=False)