jatingocodeo commited on
Commit
6ae1346
·
verified ·
1 Parent(s): 0e007bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -40
app.py CHANGED
@@ -2,31 +2,40 @@ import gradio as gr
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  from peft import PeftModel
 
 
5
 
6
- # Load model and tokenizer
7
  def load_model(model_id):
8
  # First load the base model
9
- base_model_id = "microsoft/phi-2"
10
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
11
 
12
- # Ensure tokenizer has a padding token
13
  if tokenizer.pad_token is None:
14
  tokenizer.pad_token = tokenizer.eos_token
15
 
16
  base_model = AutoModelForCausalLM.from_pretrained(
17
  base_model_id,
18
- torch_dtype=torch.float16,
19
  device_map="auto",
20
  trust_remote_code=True
21
  )
22
 
23
- # Load and merge the LoRA adapter
24
  model = PeftModel.from_pretrained(base_model, model_id)
25
  return model, tokenizer
26
 
27
- def generate_response(instruction, model, tokenizer, max_length=200, temperature=0.7, top_p=0.9):
 
 
 
 
 
28
  # Format the input text
29
- input_text = instruction.strip()
 
 
 
30
 
31
  # Tokenize input
32
  inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
@@ -45,22 +54,31 @@ def generate_response(instruction, model, tokenizer, max_length=200, temperature
45
 
46
  # Decode and return the response
47
  full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
-
49
- # Extract only the response part (what comes after the instruction)
50
- if len(input_text) < len(full_text):
51
- response = full_text[len(input_text):].strip()
52
- return response
53
- return full_text.strip()
54
 
55
  def create_demo(model_id):
56
  # Load model and tokenizer
57
  model, tokenizer = load_model(model_id)
58
 
59
- # Define the interface
60
- def process_input(instruction, max_length, temperature, top_p):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  try:
62
- return generate_response(
63
- instruction,
64
  model,
65
  tokenizer,
66
  max_length=max_length,
@@ -68,21 +86,17 @@ def create_demo(model_id):
68
  top_p=top_p
69
  )
70
  except Exception as e:
71
- return f"Error generating response: {str(e)}"
72
 
73
  # Create the interface
74
  demo = gr.Interface(
75
- fn=process_input,
76
  inputs=[
77
- gr.Textbox(
78
- label="Input Text",
79
- placeholder="Enter your text here...",
80
- lines=4
81
- ),
82
  gr.Slider(
83
  minimum=50,
84
- maximum=500,
85
- value=150,
86
  step=10,
87
  label="Maximum Length"
88
  ),
@@ -101,27 +115,21 @@ def create_demo(model_id):
101
  label="Top P"
102
  )
103
  ],
104
- outputs=gr.Textbox(label="Completion", lines=8),
105
- title="Phi-2 GRPO Model Demo",
106
- description="""This is a generative model trained using GRPO (Generative Reinforcement from Preference Optimization)
107
- on the TLDR dataset. The model was trained to generate completions of around 150 characters.
108
 
109
  You can adjust the generation parameters:
110
- - **Maximum Length**: Controls the maximum length of the generated response
111
- - **Temperature**: Higher values make the output more random, lower values make it more focused
112
- - **Top P**: Controls the cumulative probability threshold for token sampling
113
  """,
114
- examples=[
115
- ["The quick brown fox jumps over the lazy dog."],
116
- ["In this tutorial, we will explore how to build a neural network for image classification."],
117
- ["The best way to prepare for an interview is to"],
118
- ["Python is a popular programming language because"]
119
- ]
120
  )
121
  return demo
122
 
123
  if __name__ == "__main__":
124
  # Use your model ID
125
- model_id = "jatingocodeo/phi2-grpo"
126
  demo = create_demo(model_id)
127
  demo.launch()
 
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  from peft import PeftModel
5
+ from PIL import Image
6
+ import torchvision.datasets as datasets
7
 
 
8
  def load_model(model_id):
9
  # First load the base model
10
+ base_model_id = "microsoft/Phi-3-mini-4k-instruct"
11
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
12
 
13
+ # Ensure tokenizer has padding token
14
  if tokenizer.pad_token is None:
15
  tokenizer.pad_token = tokenizer.eos_token
16
 
17
  base_model = AutoModelForCausalLM.from_pretrained(
18
  base_model_id,
19
+ torch_dtype=torch.float16, # Use float16 like assignment22
20
  device_map="auto",
21
  trust_remote_code=True
22
  )
23
 
24
+ # Load the LoRA adapter
25
  model = PeftModel.from_pretrained(base_model, model_id)
26
  return model, tokenizer
27
 
28
+ def generate_description(image, model, tokenizer, max_length=100, temperature=0.7, top_p=0.9):
29
+ # Convert and resize image
30
+ if image.mode != "RGB":
31
+ image = image.convert("RGB")
32
+ image = image.resize((32, 32))
33
+
34
  # Format the input text
35
+ input_text = """Below is an image. Please describe it in detail.
36
+
37
+ Image: [IMAGE]
38
+ Description: """
39
 
40
  # Tokenize input
41
  inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
 
54
 
55
  # Decode and return the response
56
  full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
57
+ return full_text.split("Description: ")[-1].strip()
 
 
 
 
 
58
 
59
  def create_demo(model_id):
60
  # Load model and tokenizer
61
  model, tokenizer = load_model(model_id)
62
 
63
+ # Get CIFAR10 examples
64
+ cifar10_test = datasets.CIFAR10(root='./data', train=False, download=True)
65
+ examples = []
66
+ used_classes = set()
67
+
68
+ for idx in range(len(cifar10_test)):
69
+ img, label = cifar10_test[idx]
70
+ class_name = cifar10_test.classes[label]
71
+ if class_name not in used_classes:
72
+ examples.append(img)
73
+ used_classes.add(class_name)
74
+ if len(used_classes) == 10:
75
+ break
76
+
77
+ # Define the interface function
78
+ def process_image(image, max_length, temperature, top_p):
79
  try:
80
+ return generate_description(
81
+ image,
82
  model,
83
  tokenizer,
84
  max_length=max_length,
 
86
  top_p=top_p
87
  )
88
  except Exception as e:
89
+ return f"Error generating description: {str(e)}"
90
 
91
  # Create the interface
92
  demo = gr.Interface(
93
+ fn=process_image,
94
  inputs=[
95
+ gr.Image(type="pil", label="Input Image"),
 
 
 
 
96
  gr.Slider(
97
  minimum=50,
98
+ maximum=200,
99
+ value=100,
100
  step=10,
101
  label="Maximum Length"
102
  ),
 
115
  label="Top P"
116
  )
117
  ],
118
+ outputs=gr.Textbox(label="Generated Description", lines=5),
119
+ title="Image Description Generator",
120
+ description="""This model generates detailed descriptions of images.
 
121
 
122
  You can adjust the generation parameters:
123
+ - **Maximum Length**: Controls the length of the generated description
124
+ - **Temperature**: Higher values make the description more creative
125
+ - **Top P**: Controls the randomness in word selection
126
  """,
127
+ examples=[[ex] for ex in examples]
 
 
 
 
 
128
  )
129
  return demo
130
 
131
  if __name__ == "__main__":
132
  # Use your model ID
133
+ model_id = "jatingocodeo/phi-vlm"
134
  demo = create_demo(model_id)
135
  demo.launch()