arad1367 commited on
Commit
986c17a
·
verified ·
1 Parent(s): d2872f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -52
app.py CHANGED
@@ -1,59 +1,148 @@
1
  import gradio as gr
2
- import torch
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
- import spaces # Make sure this is in your requirements.txt
5
 
6
- SYSTEM_PROMPT = "You are Qwen/Qwen2.5-3B-Instruct, created by Alibaba Cloud. You are a helpful assistant."
 
 
7
 
8
- @spaces.GPU() # Correct syntax
9
- def load_model():
10
- model_name = "Qwen/Qwen2.5-3B-Instruct"
11
- model = AutoModelForCausalLM.from_pretrained(
12
- model_name,
13
- torch_dtype=torch.float16,
14
- device_map="auto"
15
- )
16
- tokenizer = AutoTokenizer.from_pretrained(model_name)
17
- return model, tokenizer
18
-
19
- model, tokenizer = load_model()
20
-
21
- def generate_response(user_input, history=None):
22
- if history is None:
23
- history = []
24
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
25
- for user_msg, bot_msg in history:
26
- messages.append({"role": "user", "content": user_msg})
27
- messages.append({"role": "assistant", "content": bot_msg})
28
- messages.append({"role": "user", "content": user_input})
29
-
30
- prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
31
- model_inputs = tokenizer([prompt_text], return_tensors="pt").to(model.device)
32
-
33
- generated_ids = model.generate(
34
- **model_inputs,
35
- max_new_tokens=512,
36
- do_sample=True,
37
- temperature=0.7,
38
- top_p=0.9
39
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- new_tokens = generated_ids[0][model_inputs.input_ids.shape[-1]:]
42
- response = tokenizer.decode(new_tokens, skip_special_tokens=True)
43
- history.append((user_input, response))
44
- return history
45
-
46
- chatbot_ui = gr.ChatInterface(
47
- fn=generate_response,
48
- title="🧠 Qwen 2.5 3B - Chatbot",
49
- description="A simple chatbot interface powered by Qwen2.5-3B-Instruct (Alibaba Cloud).",
50
- theme="soft",
51
- examples=[
52
- "How can virtual reality (VR) influence consumer behavior towards sustainability?",
53
- "What impact does sustainable packaging have on consumer purchasing decisions?",
54
- "In what ways can education promote more sustainable consumer behaviors?"
55
- ],
56
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  if __name__ == "__main__":
59
- chatbot_ui.launch()
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import random
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
5
 
6
+ # import spaces #[uncomment to use ZeroGPU]
7
+ from diffusers import DiffusionPipeline
8
+ import torch
9
 
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model_repo_id = "Qwen/Qwen2.5-3B-Instruct"
12
+ if torch.cuda.is_available():
13
+ torch_dtype = torch.float16
14
+ else:
15
+ torch_dtype = torch.float32
16
+
17
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
+ pipe = pipe.to(device)
19
+
20
+ MAX_SEED = np.iinfo(np.int32).max
21
+ MAX_IMAGE_SIZE = 1024
22
+
23
+
24
+ # @spaces.GPU #[uncomment to use ZeroGPU]
25
+ def infer(
26
+ prompt,
27
+ negative_prompt,
28
+ seed,
29
+ randomize_seed,
30
+ width,
31
+ height,
32
+ guidance_scale,
33
+ num_inference_steps,
34
+ progress=gr.Progress(track_tqdm=True),
35
+ ):
36
+ if randomize_seed:
37
+ seed = random.randint(0, MAX_SEED)
38
+
39
+ generator = torch.Generator().manual_seed(seed)
40
+
41
+ image = pipe(
42
+ prompt=prompt,
43
+ negative_prompt=negative_prompt,
44
+ guidance_scale=guidance_scale,
45
+ num_inference_steps=num_inference_steps,
46
+ width=width,
47
+ height=height,
48
+ generator=generator,
49
+ ).images[0]
50
+
51
+ return image, seed
52
+
53
+
54
+ css = """
55
+ #col-container {
56
+ margin: 0 auto;
57
+ max-width: 640px;
58
+ }
59
+ """
60
 
61
+ with gr.Blocks(css=css) as demo:
62
+ with gr.Column(elem_id="col-container"):
63
+ gr.Markdown(" # Text-to-Image Gradio Template")
64
+
65
+ with gr.Row():
66
+ prompt = gr.Text(
67
+ label="Prompt",
68
+ show_label=False,
69
+ max_lines=1,
70
+ placeholder="Enter your prompt",
71
+ container=False,
72
+ )
73
+
74
+ run_button = gr.Button("Run", scale=0, variant="primary")
75
+
76
+ result = gr.Image(label="Result", show_label=False)
77
+
78
+ with gr.Accordion("Advanced Settings", open=False):
79
+ negative_prompt = gr.Text(
80
+ label="Negative prompt",
81
+ max_lines=1,
82
+ placeholder="Enter a negative prompt",
83
+ visible=False,
84
+ )
85
+
86
+ seed = gr.Slider(
87
+ label="Seed",
88
+ minimum=0,
89
+ maximum=MAX_SEED,
90
+ step=1,
91
+ value=0,
92
+ )
93
+
94
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
95
+
96
+ with gr.Row():
97
+ width = gr.Slider(
98
+ label="Width",
99
+ minimum=256,
100
+ maximum=MAX_IMAGE_SIZE,
101
+ step=32,
102
+ value=1024, # Replace with defaults that work for your model
103
+ )
104
+
105
+ height = gr.Slider(
106
+ label="Height",
107
+ minimum=256,
108
+ maximum=MAX_IMAGE_SIZE,
109
+ step=32,
110
+ value=1024, # Replace with defaults that work for your model
111
+ )
112
+
113
+ with gr.Row():
114
+ guidance_scale = gr.Slider(
115
+ label="Guidance scale",
116
+ minimum=0.0,
117
+ maximum=10.0,
118
+ step=0.1,
119
+ value=0.0, # Replace with defaults that work for your model
120
+ )
121
+
122
+ num_inference_steps = gr.Slider(
123
+ label="Number of inference steps",
124
+ minimum=1,
125
+ maximum=50,
126
+ step=1,
127
+ value=2, # Replace with defaults that work for your model
128
+ )
129
+
130
+ gr.Examples(examples=examples, inputs=[prompt])
131
+ gr.on(
132
+ triggers=[run_button.click, prompt.submit],
133
+ fn=infer,
134
+ inputs=[
135
+ prompt,
136
+ negative_prompt,
137
+ seed,
138
+ randomize_seed,
139
+ width,
140
+ height,
141
+ guidance_scale,
142
+ num_inference_steps,
143
+ ],
144
+ outputs=[result, seed],
145
+ )
146
 
147
  if __name__ == "__main__":
148
+ demo.launch()