Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import AutoProcessor,
|
4 |
import time
|
5 |
import os
|
6 |
from huggingface_hub import login
|
@@ -32,9 +32,8 @@ def test_text_generation():
|
|
32 |
results.append(f"Error logging in: {e}")
|
33 |
return "\n".join(results)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
model_id = "meta-llama/Llama-4-8B-Instruct" # Using smaller model for faster testing
|
38 |
|
39 |
results.append(f"Loading tokenizer from {model_id}...")
|
40 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
@@ -94,13 +93,14 @@ def test_image_text_generation():
|
|
94 |
results.append(f"Error logging in: {e}")
|
95 |
return "\n".join(results)
|
96 |
|
97 |
-
|
|
|
98 |
|
99 |
results.append(f"Loading processor from {model_id}...")
|
100 |
processor = AutoProcessor.from_pretrained(model_id, token=token)
|
101 |
|
102 |
results.append(f"Loading model from {model_id}...")
|
103 |
-
model =
|
104 |
model_id,
|
105 |
token=token,
|
106 |
torch_dtype=torch.bfloat16,
|
@@ -162,11 +162,66 @@ def test_image_text_generation():
|
|
162 |
|
163 |
return "\n".join(results)
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
# Create Gradio interface
|
166 |
with gr.Blocks(title="Llama 4 Scout Test") as demo:
|
167 |
gr.Markdown("# Llama 4 Scout Test")
|
168 |
gr.Markdown("This Space tests the connection to Llama 4 and Llama 4 Scout models.")
|
169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
with gr.Tab("Text Generation Test"):
|
171 |
with gr.Row():
|
172 |
with gr.Column():
|
@@ -199,8 +254,9 @@ with gr.Blocks(title="Llama 4 Scout Test") as demo:
|
|
199 |
|
200 |
This test checks if your Space can connect to and use Llama 4 and Llama 4 Scout models.
|
201 |
|
202 |
-
- The **
|
203 |
-
- The **
|
|
|
204 |
|
205 |
If both tests pass, your Llama 4 Scout setup should work correctly.
|
206 |
""")
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from transformers import AutoProcessor, AutoModelForCausalLM, AutoTokenizer
|
4 |
import time
|
5 |
import os
|
6 |
from huggingface_hub import login
|
|
|
32 |
results.append(f"Error logging in: {e}")
|
33 |
return "\n".join(results)
|
34 |
|
35 |
+
# Use the correct model ID for Llama 4
|
36 |
+
model_id = "meta-llama/Llama-4-7b-instruct" # Correct model ID
|
|
|
37 |
|
38 |
results.append(f"Loading tokenizer from {model_id}...")
|
39 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
|
|
93 |
results.append(f"Error logging in: {e}")
|
94 |
return "\n".join(results)
|
95 |
|
96 |
+
# Use the correct model ID for Llama 4 Scout
|
97 |
+
model_id = "meta-llama/Llama-4-Scout-17B-16E-Instruct" # This should be correct
|
98 |
|
99 |
results.append(f"Loading processor from {model_id}...")
|
100 |
processor = AutoProcessor.from_pretrained(model_id, token=token)
|
101 |
|
102 |
results.append(f"Loading model from {model_id}...")
|
103 |
+
model = AutoModelForCausalLM.from_pretrained(
|
104 |
model_id,
|
105 |
token=token,
|
106 |
torch_dtype=torch.bfloat16,
|
|
|
162 |
|
163 |
return "\n".join(results)
|
164 |
|
165 |
+
# Function to list available Llama models
|
166 |
+
def list_llama_models():
|
167 |
+
results = []
|
168 |
+
results.append("=== Listing Available Llama Models ===")
|
169 |
+
|
170 |
+
try:
|
171 |
+
# Get token from environment
|
172 |
+
token = os.environ.get("HUGGINGFACE_TOKEN", "")
|
173 |
+
if token:
|
174 |
+
results.append(f"Token found: {token[:5]}...")
|
175 |
+
else:
|
176 |
+
results.append("No token found in environment variables!")
|
177 |
+
return "\n".join(results)
|
178 |
+
|
179 |
+
# Login to Hugging Face
|
180 |
+
try:
|
181 |
+
login(token=token)
|
182 |
+
results.append("Successfully logged in to Hugging Face Hub")
|
183 |
+
except Exception as e:
|
184 |
+
results.append(f"Error logging in: {e}")
|
185 |
+
return "\n".join(results)
|
186 |
+
|
187 |
+
# List models from the meta-llama organization
|
188 |
+
from huggingface_hub import HfApi
|
189 |
+
api = HfApi(token=token)
|
190 |
+
|
191 |
+
results.append("Fetching models from meta-llama organization...")
|
192 |
+
models = api.list_models(author="meta-llama")
|
193 |
+
|
194 |
+
results.append(f"Found {len(models)} models:")
|
195 |
+
for model in models:
|
196 |
+
results.append(f"- {model.id}")
|
197 |
+
|
198 |
+
results.append("MODEL LISTING SUCCESSFUL!")
|
199 |
+
|
200 |
+
except Exception as e:
|
201 |
+
results.append(f"Error listing models: {e}")
|
202 |
+
results.append(traceback.format_exc())
|
203 |
+
results.append("MODEL LISTING FAILED!")
|
204 |
+
|
205 |
+
return "\n".join(results)
|
206 |
+
|
207 |
# Create Gradio interface
|
208 |
with gr.Blocks(title="Llama 4 Scout Test") as demo:
|
209 |
gr.Markdown("# Llama 4 Scout Test")
|
210 |
gr.Markdown("This Space tests the connection to Llama 4 and Llama 4 Scout models.")
|
211 |
|
212 |
+
with gr.Tab("List Available Models"):
|
213 |
+
with gr.Row():
|
214 |
+
with gr.Column():
|
215 |
+
list_models_button = gr.Button("List Available Llama Models")
|
216 |
+
with gr.Column():
|
217 |
+
models_result = gr.Textbox(label="Available Models", lines=20)
|
218 |
+
|
219 |
+
list_models_button.click(
|
220 |
+
fn=list_llama_models,
|
221 |
+
inputs=[],
|
222 |
+
outputs=[models_result]
|
223 |
+
)
|
224 |
+
|
225 |
with gr.Tab("Text Generation Test"):
|
226 |
with gr.Row():
|
227 |
with gr.Column():
|
|
|
254 |
|
255 |
This test checks if your Space can connect to and use Llama 4 and Llama 4 Scout models.
|
256 |
|
257 |
+
- The **List Available Models** tab shows all models available from meta-llama
|
258 |
+
- The **Text Generation Test** uses Llama 4 7B for basic text generation
|
259 |
+
- The **Image-Text Generation Test** uses Llama 4 Scout for image-text generation
|
260 |
|
261 |
If both tests pass, your Llama 4 Scout setup should work correctly.
|
262 |
""")
|