Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,248 +1,40 @@
|
|
| 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
|
| 7 |
-
import requests
|
| 8 |
-
from PIL import Image
|
| 9 |
-
from io import BytesIO
|
| 10 |
-
import base64
|
| 11 |
-
import traceback
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
# Get token from environment
|
| 20 |
-
token = os.environ.get("HUGGINGFACE_TOKEN", "")
|
| 21 |
-
if token:
|
| 22 |
-
results.append(f"Token found: {token[:5]}...")
|
| 23 |
-
else:
|
| 24 |
-
results.append("No token found in environment variables!")
|
| 25 |
-
return "\n".join(results)
|
| 26 |
-
|
| 27 |
-
# Login to Hugging Face
|
| 28 |
-
try:
|
| 29 |
-
login(token=token)
|
| 30 |
-
results.append("Successfully logged in to Hugging Face Hub")
|
| 31 |
-
except Exception as e:
|
| 32 |
-
results.append(f"Error logging in: {e}")
|
| 33 |
-
return "\n".join(results)
|
| 34 |
-
|
| 35 |
-
# Use Qwen model
|
| 36 |
-
model_id = "Qwen/Qwen2-7B-Instruct" # Smaller model for testing
|
| 37 |
-
|
| 38 |
-
results.append(f"Loading tokenizer from {model_id}...")
|
| 39 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 40 |
-
|
| 41 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 42 |
-
|
| 43 |
-
results.append(f"Loading model from {model_id}...")
|
| 44 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 45 |
-
model_id,
|
| 46 |
-
torch_dtype=torch.bfloat16,
|
| 47 |
-
device_map="auto"
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
results.append("Model and tokenizer loaded successfully!")
|
| 51 |
-
|
| 52 |
-
# Simple prompt
|
| 53 |
-
prompt = "Write a short poem about artificial intelligence."
|
| 54 |
-
|
| 55 |
-
results.append(f"Generating text for prompt: '{prompt}'")
|
| 56 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 57 |
-
|
| 58 |
-
start_time = time.time()
|
| 59 |
-
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 60 |
-
end_time = time.time()
|
| 61 |
-
|
| 62 |
-
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 63 |
-
|
| 64 |
-
results.append(f"Generation completed in {end_time - start_time:.2f} seconds")
|
| 65 |
-
results.append(f"Result: {result}")
|
| 66 |
-
results.append("TEXT TEST SUCCESSFUL!")
|
| 67 |
-
|
| 68 |
-
except Exception as e:
|
| 69 |
-
results.append(f"Error in text generation test: {e}")
|
| 70 |
-
results.append(traceback.format_exc())
|
| 71 |
-
results.append("TEXT TEST FAILED!")
|
| 72 |
-
|
| 73 |
-
return "\n".join(results)
|
| 74 |
-
|
| 75 |
-
# Function to run the Qwen image-text test
|
| 76 |
-
def test_image_text_generation():
|
| 77 |
-
results = []
|
| 78 |
-
results.append("=== Testing Qwen Image-Text Generation ===")
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
# Get token from environment
|
| 82 |
-
token = os.environ.get("HUGGINGFACE_TOKEN", "")
|
| 83 |
-
if token:
|
| 84 |
-
results.append(f"Token found: {token[:5]}...")
|
| 85 |
-
else:
|
| 86 |
-
results.append("No token found in environment variables!")
|
| 87 |
-
return "\n".join(results)
|
| 88 |
-
|
| 89 |
-
# Login to Hugging Face
|
| 90 |
-
try:
|
| 91 |
-
login(token=token)
|
| 92 |
-
results.append("Successfully logged in to Hugging Face Hub")
|
| 93 |
-
except Exception as e:
|
| 94 |
-
results.append(f"Error logging in: {e}")
|
| 95 |
-
return "\n".join(results)
|
| 96 |
-
|
| 97 |
-
# Use the Qwen VL model
|
| 98 |
-
model_id = "Qwen/Qwen2-VL-7B" # Visual language model
|
| 99 |
-
|
| 100 |
-
results.append(f"Loading processor and model from {model_id}...")
|
| 101 |
-
|
| 102 |
-
# Load processor and model with the correct classes
|
| 103 |
-
from transformers import Qwen2VLProcessor, Qwen2VLForConditionalGeneration
|
| 104 |
-
|
| 105 |
-
processor = Qwen2VLProcessor.from_pretrained(model_id)
|
| 106 |
-
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 107 |
-
model_id,
|
| 108 |
-
torch_dtype=torch.bfloat16,
|
| 109 |
-
device_map="auto"
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
results.append("Model and processor loaded successfully!")
|
| 113 |
-
|
| 114 |
-
# Load a test image
|
| 115 |
-
results.append("Loading test image...")
|
| 116 |
-
response = requests.get("https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg")
|
| 117 |
-
img = Image.open(BytesIO(response.content))
|
| 118 |
-
results.append(f"Image loaded: {img.size}")
|
| 119 |
-
|
| 120 |
-
# Simple prompt
|
| 121 |
-
prompt = "Describe this image in two sentences."
|
| 122 |
-
|
| 123 |
-
results.append(f"Creating prompt with image: '{prompt}'")
|
| 124 |
-
|
| 125 |
-
# Process inputs
|
| 126 |
-
inputs = processor(text=prompt, images=img, return_tensors="pt").to(model.device)
|
| 127 |
-
|
| 128 |
-
results.append("Generating response...")
|
| 129 |
-
start_time = time.time()
|
| 130 |
-
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 131 |
-
end_time = time.time()
|
| 132 |
-
|
| 133 |
-
result = processor.decode(outputs[0], skip_special_tokens=True)
|
| 134 |
-
|
| 135 |
-
results.append(f"Generation completed in {end_time - start_time:.2f} seconds")
|
| 136 |
-
results.append(f"Result: {result}")
|
| 137 |
-
results.append("IMAGE-TEXT TEST SUCCESSFUL!")
|
| 138 |
-
|
| 139 |
-
except Exception as e:
|
| 140 |
-
results.append(f"Error in image-text generation test: {e}")
|
| 141 |
-
results.append(traceback.format_exc())
|
| 142 |
-
results.append("IMAGE-TEXT TEST FAILED!")
|
| 143 |
-
|
| 144 |
-
return "\n".join(results)
|
| 145 |
-
|
| 146 |
-
# Function to list available Qwen models
|
| 147 |
-
def list_qwen_models():
|
| 148 |
-
results = []
|
| 149 |
-
results.append("=== Listing Available Qwen Models ===")
|
| 150 |
-
|
| 151 |
-
try:
|
| 152 |
-
# Get token from environment
|
| 153 |
-
token = os.environ.get("HUGGINGFACE_TOKEN", "")
|
| 154 |
-
if token:
|
| 155 |
-
results.append(f"Token found: {token[:5]}...")
|
| 156 |
-
else:
|
| 157 |
-
results.append("No token found in environment variables!")
|
| 158 |
-
return "\n".join(results)
|
| 159 |
-
|
| 160 |
-
# Login to Hugging Face
|
| 161 |
-
try:
|
| 162 |
-
login(token=token)
|
| 163 |
-
results.append("Successfully logged in to Hugging Face Hub")
|
| 164 |
-
except Exception as e:
|
| 165 |
-
results.append(f"Error logging in: {e}")
|
| 166 |
-
return "\n".join(results)
|
| 167 |
-
|
| 168 |
-
# List models from the Qwen organization
|
| 169 |
-
from huggingface_hub import HfApi
|
| 170 |
-
api = HfApi(token=token)
|
| 171 |
-
|
| 172 |
-
results.append("Fetching models from Qwen organization...")
|
| 173 |
-
results.append("Available Qwen models:")
|
| 174 |
-
|
| 175 |
-
model_count = 0
|
| 176 |
-
for model in api.list_models(author="Qwen"):
|
| 177 |
-
results.append(f"- {model.id}")
|
| 178 |
-
model_count += 1
|
| 179 |
-
|
| 180 |
-
results.append(f"Total Qwen models found: {model_count}")
|
| 181 |
-
results.append("MODEL LISTING SUCCESSFUL!")
|
| 182 |
-
|
| 183 |
-
except Exception as e:
|
| 184 |
-
results.append(f"Error listing models: {e}")
|
| 185 |
-
results.append(traceback.format_exc())
|
| 186 |
-
results.append("MODEL LISTING FAILED!")
|
| 187 |
-
|
| 188 |
-
return "\n".join(results)
|
| 189 |
-
|
| 190 |
-
# Create Gradio interface
|
| 191 |
-
with gr.Blocks(title="Qwen Model Test") as demo:
|
| 192 |
-
gr.Markdown("# Qwen Model Test")
|
| 193 |
-
gr.Markdown("This Space tests the connection to Qwen models.")
|
| 194 |
-
|
| 195 |
-
with gr.Tab("List Qwen Models"):
|
| 196 |
with gr.Row():
|
| 197 |
with gr.Column():
|
| 198 |
-
|
| 199 |
with gr.Column():
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
inputs=[],
|
| 205 |
-
outputs=[
|
| 206 |
)
|
| 207 |
-
|
| 208 |
-
with gr.Tab("Text Generation Test"):
|
| 209 |
-
with gr.Row():
|
| 210 |
-
with gr.Column():
|
| 211 |
-
text_test_button = gr.Button("Run Text Generation Test")
|
| 212 |
-
with gr.Column():
|
| 213 |
-
text_test_result = gr.Textbox(label="Test Results", lines=20)
|
| 214 |
-
|
| 215 |
-
text_test_button.click(
|
| 216 |
-
fn=test_text_generation,
|
| 217 |
-
inputs=[],
|
| 218 |
-
outputs=[text_test_result]
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
with gr.Tab("Image-Text Generation Test"):
|
| 222 |
-
with gr.Row():
|
| 223 |
-
with gr.Column():
|
| 224 |
-
image_test_button = gr.Button("Run Image-Text Generation Test")
|
| 225 |
-
with gr.Column():
|
| 226 |
-
image_test_result = gr.Textbox(label="Test Results", lines=20)
|
| 227 |
-
|
| 228 |
-
image_test_button.click(
|
| 229 |
-
fn=test_image_text_generation,
|
| 230 |
-
inputs=[],
|
| 231 |
-
outputs=[image_test_result]
|
| 232 |
-
)
|
| 233 |
-
|
| 234 |
-
with gr.Tab("About"):
|
| 235 |
-
gr.Markdown("""
|
| 236 |
-
## About This Test
|
| 237 |
-
|
| 238 |
-
This test checks if your Space can connect to and use Qwen models.
|
| 239 |
-
|
| 240 |
-
- The **List Qwen Models** tab shows all models available from Qwen
|
| 241 |
-
- The **Text Generation Test** uses Qwen2-7B-Instruct for basic text generation
|
| 242 |
-
- The **Image-Text Generation Test** uses Qwen2-VL-7B for image-text generation
|
| 243 |
-
|
| 244 |
-
If both tests pass, your Qwen setup should work correctly.
|
| 245 |
-
""")
|
| 246 |
|
| 247 |
# Launch the app
|
| 248 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Create a simple interface
|
| 5 |
+
with gr.Blocks(title="Simple Qwen Test") as demo:
|
| 6 |
+
gr.Markdown("# Simple Qwen Test")
|
| 7 |
+
gr.Markdown("This is a minimal test to check if the Space is working.")
|
| 8 |
|
| 9 |
+
with gr.Tab("Basic Test"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
with gr.Row():
|
| 11 |
with gr.Column():
|
| 12 |
+
test_button = gr.Button("Run Basic Test")
|
| 13 |
with gr.Column():
|
| 14 |
+
test_result = gr.Textbox(label="Test Results", lines=10)
|
| 15 |
+
|
| 16 |
+
def basic_test():
|
| 17 |
+
try:
|
| 18 |
+
# Just print system info
|
| 19 |
+
import sys
|
| 20 |
+
import transformers
|
| 21 |
+
|
| 22 |
+
result = []
|
| 23 |
+
result.append(f"Python version: {sys.version}")
|
| 24 |
+
result.append(f"PyTorch version: {torch.__version__}")
|
| 25 |
+
result.append(f"Transformers version: {transformers.__version__}")
|
| 26 |
+
result.append("Basic test successful!")
|
| 27 |
+
|
| 28 |
+
return "\n".join(result)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
import traceback
|
| 31 |
+
return f"Error: {str(e)}\n\n{traceback.format_exc()}"
|
| 32 |
+
|
| 33 |
+
test_button.click(
|
| 34 |
+
fn=basic_test,
|
| 35 |
inputs=[],
|
| 36 |
+
outputs=[test_result]
|
| 37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Launch the app
|
| 40 |
demo.launch()
|