cpg716 commited on
Commit
726139a
·
verified ·
1 Parent(s): 243e1dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -183
app.py CHANGED
@@ -3,6 +3,7 @@ import torch
3
  import sys
4
  import traceback
5
  import os
 
6
 
7
  def system_info():
8
  try:
@@ -26,162 +27,74 @@ def system_info():
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:
@@ -190,7 +103,7 @@ def test_phi3_with_image():
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,56 +118,28 @@ with gr.Blocks(title="StaffManager AI Assistant") as demo:
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
 
3
  import sys
4
  import traceback
5
  import os
6
+ from huggingface_hub import login
7
 
8
  def system_info():
9
  try:
 
27
  except Exception as e:
28
  return f"Error: {str(e)}\n\n{traceback.format_exc()}"
29
 
30
+ def test_gemma3():
31
  try:
32
  result = []
33
+ result.append("Testing Gemma 3 model...")
34
 
35
+ # Get token from environment
36
+ token = os.environ.get("HUGGINGFACE_TOKEN", "")
37
+ if token:
38
+ result.append(f"Token found: {token[:5]}...")
39
+ else:
40
+ result.append("No token found in environment variables!")
41
+ return "\n".join(result)
42
+
43
+ # Login to Hugging Face
44
+ try:
45
+ login(token=token)
46
+ result.append("Successfully logged in to Hugging Face Hub")
47
+ except Exception as e:
48
+ result.append(f"Error logging in: {e}")
49
+ return "\n".join(result)
50
+
51
+ # Use Gemma 3 GGUF model
52
+ model_id = "google/gemma-3-27b-it-qat-q4_0-gguf"
53
+ model_filename = "gemma-3-27b-it-qat-q4_0.gguf"
54
+
55
+ result.append(f"Downloading {model_id} if not already present...")
56
+ from huggingface_hub import hf_hub_download
57
+
58
+ model_path = hf_hub_download(
59
+ repo_id=model_id,
60
+ filename=model_filename,
61
+ token=token
62
  )
63
+ result.append(f"Model downloaded to: {model_path}")
64
+
65
+ # Load the model
66
+ result.append("Loading model...")
67
+ try:
68
+ import llama_cpp
69
+ except ImportError:
70
+ result.append("llama-cpp-python not installed. Installing now...")
71
+ import subprocess
72
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python"])
73
+ import llama_cpp
74
+
75
+ from llama_cpp import Llama
76
+
77
+ llm = Llama(
78
+ model_path=model_path,
79
+ n_ctx=2048, # Context window size
80
+ n_gpu_layers=-1 # Use all available GPU layers
81
  )
82
 
83
+ # Generate text
84
  result.append("Generating text...")
85
  prompt = "Write a short poem about artificial intelligence."
86
 
87
+ output = llm(
88
+ prompt,
89
+ max_tokens=100,
90
+ temperature=0.7,
91
+ top_p=0.95,
92
+ echo=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  )
94
 
95
+ generated_text = output["choices"][0]["text"]
96
+ result.append(f"Generated text: {generated_text}")
97
+ result.append("Gemma 3 test successful!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  return "\n".join(result)
100
  except Exception as e:
 
103
  # Create Gradio interface
104
  with gr.Blocks(title="StaffManager AI Assistant") as demo:
105
  gr.Markdown("# StaffManager AI Assistant")
106
+ gr.Markdown("Testing Gemma 3 model for StaffManager application.")
107
 
108
  with gr.Tab("System Info"):
109
  with gr.Row():
 
118
  outputs=[info_result]
119
  )
120
 
121
+ with gr.Tab("Gemma 3 Test"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  with gr.Row():
123
  with gr.Column():
124
+ gemma_button = gr.Button("Test Gemma 3")
125
  with gr.Column():
126
+ gemma_result = gr.Textbox(label="Test Results", lines=20)
127
 
128
+ gemma_button.click(
129
+ fn=test_gemma3,
130
  inputs=[],
131
+ outputs=[gemma_result]
132
  )
133
 
134
  with gr.Tab("About"):
135
  gr.Markdown("""
136
  ## About StaffManager AI Assistant
137
 
138
+ This Space tests the Gemma 3 model for the StaffManager application.
139
 
140
+ - **Gemma 3**: Google's 27B parameter model in GGUF format for efficient inference
 
 
141
 
142
+ This model requires authentication with a Hugging Face token that has been granted access to the model.
143
  """)
144
 
145
  # Launch the app