cpg716 commited on
Commit
243e1dc
·
verified ·
1 Parent(s): 59fa1c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +193 -52
app.py CHANGED
@@ -3,7 +3,6 @@ import torch
3
  import sys
4
  import traceback
5
  import os
6
- from huggingface_hub import hf_hub_download
7
 
8
  def system_info():
9
  try:
@@ -27,68 +26,171 @@ def system_info():
27
  except Exception as e:
28
  return f"Error: {str(e)}\n\n{traceback.format_exc()}"
29
 
30
- def test_gemma_gguf():
31
  try:
32
  result = []
33
- result.append("Testing Gemma 3 GGUF model...")
34
-
35
- # First, check if llama-cpp-python is installed
36
- try:
37
- import llama_cpp
38
- result.append(f"llama_cpp version: {llama_cpp.__version__}")
39
- except ImportError:
40
- result.append("llama-cpp-python not installed. Installing now...")
41
- import subprocess
42
- subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python"])
43
- import llama_cpp
44
- result.append(f"llama_cpp version: {llama_cpp.__version__}")
45
-
46
- # Download the model if not already downloaded
47
- model_id = "google/gemma-3-27b-it-qat-q4_0-gguf"
48
- model_filename = "gemma-3-27b-it-qat-q4_0.gguf"
49
-
50
- result.append(f"Downloading {model_id} if not already present...")
51
- model_path = hf_hub_download(
52
- repo_id=model_id,
53
- filename=model_filename,
54
- resume_download=True
55
- )
56
- result.append(f"Model downloaded to: {model_path}")
57
 
58
- # Load the model
59
- result.append("Loading model...")
60
- from llama_cpp import Llama
61
 
62
- llm = Llama(
63
- model_path=model_path,
64
- n_ctx=2048, # Context window size
65
- n_gpu_layers=-1 # Use all available GPU layers
 
 
 
 
 
 
 
 
 
66
  )
67
 
68
- # Generate text
69
  result.append("Generating text...")
70
  prompt = "Write a short poem about artificial intelligence."
71
 
72
- output = llm(
73
- prompt,
74
- max_tokens=100,
75
- temperature=0.7,
76
- top_p=0.95,
77
- echo=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  )
79
 
80
- generated_text = output["choices"][0]["text"]
81
- result.append(f"Generated text: {generated_text}")
82
- result.append("Gemma 3 GGUF test successful!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  return "\n".join(result)
85
  except Exception as e:
86
  return f"Error: {str(e)}\n\n{traceback.format_exc()}"
87
 
88
  # Create Gradio interface
89
- with gr.Blocks(title="Gemma 3 GGUF Test") as demo:
90
- gr.Markdown("# Gemma 3 GGUF Test")
91
- gr.Markdown("Testing Gemma 3 model in GGUF format using llama-cpp-python.")
92
 
93
  with gr.Tab("System Info"):
94
  with gr.Row():
@@ -103,18 +205,57 @@ with gr.Blocks(title="Gemma 3 GGUF Test") as demo:
103
  outputs=[info_result]
104
  )
105
 
106
- with gr.Tab("Gemma 3 GGUF Test"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  with gr.Row():
108
  with gr.Column():
109
- gemma_button = gr.Button("Test Gemma 3 GGUF")
110
  with gr.Column():
111
- gemma_result = gr.Textbox(label="Test Results", lines=20)
112
 
113
- gemma_button.click(
114
- fn=test_gemma_gguf,
115
  inputs=[],
116
- outputs=[gemma_result]
117
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  # Launch the app
120
  demo.launch()
 
3
  import sys
4
  import traceback
5
  import os
 
6
 
7
  def system_info():
8
  try:
 
26
  except Exception as e:
27
  return f"Error: {str(e)}\n\n{traceback.format_exc()}"
28
 
29
+ def test_phi3_mini():
30
  try:
31
  result = []
32
+ result.append("Testing Phi-3 Mini model...")
33
+
34
+ # Use Phi-3 Mini model with 4-bit quantization
35
+ model_id = "microsoft/Phi-3-mini-4k-instruct"
36
+
37
+ result.append(f"Loading tokenizer from {model_id}...")
38
+ from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
41
 
42
+ result.append("Loading model with quantization...")
43
+ from transformers import BitsAndBytesConfig
44
+
45
+ quantization_config = BitsAndBytesConfig(
46
+ load_in_4bit=True,
47
+ bnb_4bit_compute_dtype=torch.float16,
48
+ bnb_4bit_quant_type="nf4"
49
+ )
50
+
51
+ model = AutoModelForCausalLM.from_pretrained(
52
+ model_id,
53
+ quantization_config=quantization_config,
54
+ device_map="auto"
55
  )
56
 
 
57
  result.append("Generating text...")
58
  prompt = "Write a short poem about artificial intelligence."
59
 
60
+ # Format prompt for Phi-3
61
+ messages = [
62
+ {"role": "user", "content": prompt}
63
+ ]
64
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False)
65
+
66
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
67
+ outputs = model.generate(**inputs, max_new_tokens=100)
68
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
69
+
70
+ # Extract the assistant's response
71
+ if "<assistant>" in generated_text and "</assistant>" in generated_text:
72
+ response = generated_text.split("<assistant>")[1].split("</assistant>")[0].strip()
73
+ else:
74
+ response = generated_text.replace(prompt, "").strip()
75
+
76
+ result.append(f"Generated text: {response}")
77
+ result.append("Phi-3 Mini test successful!")
78
+
79
+ return "\n".join(result)
80
+ except Exception as e:
81
+ return f"Error: {str(e)}\n\n{traceback.format_exc()}"
82
+
83
+ def test_image_classification():
84
+ try:
85
+ result = []
86
+ result.append("Testing image classification...")
87
+
88
+ # Use a lightweight vision model
89
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
90
+
91
+ result.append("Loading image processor and model...")
92
+ processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
93
+ model = AutoModelForImageClassification.from_pretrained("microsoft/resnet-50")
94
+
95
+ result.append("Loading test image...")
96
+ import requests
97
+ from PIL import Image
98
+ from io import BytesIO
99
+
100
+ response = requests.get("https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg")
101
+ img = Image.open(BytesIO(response.content))
102
+
103
+ result.append("Processing image...")
104
+ inputs = processor(images=img, return_tensors="pt")
105
+ outputs = model(**inputs)
106
+
107
+ # Get predicted class
108
+ predicted_class_idx = outputs.logits.argmax(-1).item()
109
+ predicted_class = model.config.id2label[predicted_class_idx]
110
+
111
+ result.append(f"Predicted class: {predicted_class}")
112
+ result.append("Image classification test successful!")
113
+
114
+ return "\n".join(result)
115
+ except Exception as e:
116
+ return f"Error: {str(e)}\n\n{traceback.format_exc()}"
117
+
118
+ def test_phi3_with_image():
119
+ try:
120
+ result = []
121
+ result.append("Testing Phi-3 Mini with image description...")
122
+
123
+ # First, classify the image
124
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
125
+ import requests
126
+ from PIL import Image
127
+ from io import BytesIO
128
+
129
+ result.append("Loading image and classifying it...")
130
+ img_processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
131
+ img_model = AutoModelForImageClassification.from_pretrained("microsoft/resnet-50")
132
+
133
+ response = requests.get("https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg")
134
+ img = Image.open(BytesIO(response.content))
135
+
136
+ inputs = img_processor(images=img, return_tensors="pt")
137
+ outputs = img_model(**inputs)
138
+
139
+ predicted_class_idx = outputs.logits.argmax(-1).item()
140
+ predicted_class = img_model.config.id2label[predicted_class_idx]
141
+
142
+ result.append(f"Image classified as: {predicted_class}")
143
+
144
+ # Now use Phi-3 to describe the image based on the classification
145
+ result.append("Loading Phi-3 Mini model...")
146
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
147
+
148
+ model_id = "microsoft/Phi-3-mini-4k-instruct"
149
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
150
+
151
+ quantization_config = BitsAndBytesConfig(
152
+ load_in_4bit=True,
153
+ bnb_4bit_compute_dtype=torch.float16,
154
+ bnb_4bit_quant_type="nf4"
155
+ )
156
+
157
+ model = AutoModelForCausalLM.from_pretrained(
158
+ model_id,
159
+ quantization_config=quantization_config,
160
+ device_map="auto"
161
  )
162
 
163
+ # Create a prompt that includes the image classification
164
+ prompt = f"I have an image that contains a {predicted_class}. Please write a detailed description of what this might look like, and explain some interesting facts about {predicted_class}."
165
+
166
+ # Format prompt for Phi-3
167
+ messages = [
168
+ {"role": "user", "content": prompt}
169
+ ]
170
+ formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False)
171
+
172
+ result.append("Generating description based on image classification...")
173
+ inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
174
+ outputs = model.generate(**inputs, max_new_tokens=200)
175
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
176
+
177
+ # Extract the assistant's response
178
+ if "<assistant>" in generated_text and "</assistant>" in generated_text:
179
+ response = generated_text.split("<assistant>")[1].split("</assistant>")[0].strip()
180
+ else:
181
+ response = generated_text.replace(formatted_prompt, "").strip()
182
+
183
+ result.append(f"Generated description: {response}")
184
+ result.append("Phi-3 with image test successful!")
185
 
186
  return "\n".join(result)
187
  except Exception as e:
188
  return f"Error: {str(e)}\n\n{traceback.format_exc()}"
189
 
190
  # Create Gradio interface
191
+ with gr.Blocks(title="StaffManager AI Assistant") as demo:
192
+ gr.Markdown("# StaffManager AI Assistant")
193
+ gr.Markdown("Testing open-access models for text and image processing.")
194
 
195
  with gr.Tab("System Info"):
196
  with gr.Row():
 
205
  outputs=[info_result]
206
  )
207
 
208
+ with gr.Tab("Text Generation"):
209
+ with gr.Row():
210
+ with gr.Column():
211
+ phi3_button = gr.Button("Generate Text with Phi-3 Mini")
212
+ with gr.Column():
213
+ phi3_result = gr.Textbox(label="Generated Text", lines=20)
214
+
215
+ phi3_button.click(
216
+ fn=test_phi3_mini,
217
+ inputs=[],
218
+ outputs=[phi3_result]
219
+ )
220
+
221
+ with gr.Tab("Image Classification"):
222
+ with gr.Row():
223
+ with gr.Column():
224
+ image_button = gr.Button("Classify Sample Image")
225
+ with gr.Column():
226
+ image_result = gr.Textbox(label="Classification Results", lines=20)
227
+
228
+ image_button.click(
229
+ fn=test_image_classification,
230
+ inputs=[],
231
+ outputs=[image_result]
232
+ )
233
+
234
+ with gr.Tab("Image Description"):
235
  with gr.Row():
236
  with gr.Column():
237
+ combined_button = gr.Button("Generate Image Description")
238
  with gr.Column():
239
+ combined_result = gr.Textbox(label="Description Results", lines=20)
240
 
241
+ combined_button.click(
242
+ fn=test_phi3_with_image,
243
  inputs=[],
244
+ outputs=[combined_result]
245
  )
246
+
247
+ with gr.Tab("About"):
248
+ gr.Markdown("""
249
+ ## About StaffManager AI Assistant
250
+
251
+ This Space demonstrates AI capabilities for StaffManager using open-access models:
252
+
253
+ - **Text Generation**: Uses Microsoft's Phi-3 Mini model
254
+ - **Image Classification**: Uses Microsoft's ResNet-50 model
255
+ - **Image Description**: Combines both models to classify and describe images
256
+
257
+ These models are completely open-access and don't require any special authentication.
258
+ """)
259
 
260
  # Launch the app
261
  demo.launch()