Update app.py
Browse files
app.py
CHANGED
@@ -1,129 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import google.generativeai as genai
|
6 |
-
from PIL import Image
|
7 |
-
import ast
|
8 |
-
import re
|
9 |
-
|
10 |
-
def process_file(api_key, file, instructions):
|
11 |
-
genai.configure(api_key=api_key)
|
12 |
-
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
return [generate_error_image(str(e))]*3
|
21 |
-
|
22 |
-
# Enhanced prompt with strict plotting requirements
|
23 |
-
prompt = f"""Generate 3 matplotlib codes with these rules:
|
24 |
-
1. Use ONLY these variables: df (DataFrame), plt
|
25 |
-
2. Each visualization MUST:
|
26 |
-
- Plot actual data from df
|
27 |
-
- Include title, axis labels, and data labels if needed
|
28 |
-
- Use clear color schemes
|
29 |
-
- Avoid empty plots
|
30 |
-
3. Code structure:
|
31 |
-
plt.figure(figsize=(16,9), dpi=120)
|
32 |
-
plt.style.use('ggplot')
|
33 |
-
# Plotting code using df columns: {list(df.columns)}
|
34 |
-
plt.tight_layout()
|
35 |
-
|
36 |
-
Sample data: {df.head(3).to_dict()}
|
37 |
-
User instructions: {instructions or 'General insights'}
|
38 |
-
|
39 |
-
Format EXACTLY as:
|
40 |
-
# Visualization 1
|
41 |
-
[complete code]
|
42 |
-
"""
|
43 |
-
|
44 |
-
try:
|
45 |
-
response = model.generate_content(prompt)
|
46 |
-
code_blocks = re.split(r'# Visualization \d+', response.text)[1:4]
|
47 |
-
except Exception as e:
|
48 |
-
return [generate_error_image("API Error")]*3
|
49 |
-
|
50 |
-
visualizations = []
|
51 |
-
for i, block in enumerate(code_blocks, 1):
|
52 |
try:
|
53 |
-
|
54 |
-
cleaned_code = sanitize_code(block, df.columns)
|
55 |
-
|
56 |
-
# Validate and execute
|
57 |
-
ast.parse(cleaned_code)
|
58 |
-
img = execute_plot_code(cleaned_code, df)
|
59 |
-
visualizations.append(img)
|
60 |
except Exception as e:
|
61 |
-
|
62 |
-
visualizations.append(generate_error_image(f"Plot {i} Error"))
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
def execute_plot_code(code, df):
|
90 |
-
"""Safely execute plotting code"""
|
91 |
-
buf = io.BytesIO()
|
92 |
-
plt.figure(figsize=(16, 9), dpi=120)
|
93 |
-
plt.style.use('ggplot')
|
94 |
-
|
95 |
-
try:
|
96 |
-
exec(code, {'df': df, 'plt': plt})
|
97 |
-
plt.tight_layout()
|
98 |
-
plt.savefig(buf, format='png', bbox_inches='tight')
|
99 |
-
buf.seek(0)
|
100 |
-
return Image.open(buf)
|
101 |
-
finally:
|
102 |
-
plt.close()
|
103 |
-
|
104 |
-
def generate_error_image(message):
|
105 |
-
"""Create error indication image"""
|
106 |
-
img = Image.new('RGB', (1920, 1080), color=(73, 109, 137))
|
107 |
-
return img
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
gr.Markdown("# Professional Data Visualizer")
|
112 |
|
113 |
with gr.Row():
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
|
118 |
-
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
process_file,
|
125 |
-
inputs=[api_key, file, instructions],
|
126 |
-
outputs=outputs
|
127 |
)
|
128 |
|
129 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from awq import AutoAWQForCausalLM
|
3 |
+
from transformers import AutoTokenizer, AutoConfig
|
4 |
+
from huggingface_hub import HfApi, login
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def quantize_model(
|
7 |
+
model_id: str,
|
8 |
+
hf_token: str,
|
9 |
+
repo_name: str,
|
10 |
+
progress=gr.Progress(track_tqdm=True)
|
11 |
+
):
|
12 |
try:
|
13 |
+
# Validate credentials first
|
14 |
+
login(token=hf_token, add_to_git_credential=True)
|
15 |
+
api = HfApi(token=hf_token)
|
16 |
+
|
17 |
+
# Check model accessibility
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
try:
|
19 |
+
api.model_info(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
except Exception as e:
|
21 |
+
raise ValueError(f"Model access error: {str(e)}. Check:\n1. Token permissions\n2. Model existence\n3. Accept model terms at https://huggingface.co/{model_id}")
|
|
|
22 |
|
23 |
+
# Load config with proper auth
|
24 |
+
config = AutoConfig.from_pretrained(
|
25 |
+
model_id,
|
26 |
+
token=hf_token,
|
27 |
+
trust_remote_code=True
|
28 |
+
)
|
29 |
+
|
30 |
+
# Handle Llama 3 rope_scaling
|
31 |
+
if hasattr(config, 'rope_scaling') and isinstance(config.rope_scaling, dict):
|
32 |
+
config.rope_scaling = {
|
33 |
+
"type": config.rope_scaling.get("rope_type", "linear"),
|
34 |
+
"factor": config.rope_scaling.get("factor", 1.0)
|
35 |
+
}
|
36 |
+
|
37 |
+
# Load model with validated credentials
|
38 |
+
model = AutoAWQForCausalLM.from_pretrained(
|
39 |
+
model_id,
|
40 |
+
config=config,
|
41 |
+
token=hf_token,
|
42 |
+
trust_remote_code=True,
|
43 |
+
device_map="auto"
|
44 |
+
)
|
45 |
+
|
46 |
+
# Load tokenizer with same credentials
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
48 |
+
model_id,
|
49 |
+
token=hf_token,
|
50 |
+
trust_remote_code=True
|
51 |
+
)
|
52 |
+
|
53 |
+
# Quantize with auto-detected settings
|
54 |
+
model.quantize(tokenizer, quant_config={
|
55 |
+
"zero_point": True,
|
56 |
+
"q_group_size": 128,
|
57 |
+
"w_bit": 4,
|
58 |
+
"version": "GEMM" if "llama" in model_id.lower() else "GEMV"
|
59 |
+
})
|
60 |
+
|
61 |
+
# Save and push
|
62 |
+
save_path = f"{model_id.split('/')[-1]}-awq"
|
63 |
+
model.save_quantized(save_path)
|
64 |
+
model.push_to_hub(repo_name, token=hf_token)
|
65 |
+
|
66 |
+
return f"✅ Success!\nSaved: {save_path}\nPushed to: {repo_name}"
|
67 |
|
68 |
+
except Exception as e:
|
69 |
+
return f"❌ Critical Error:\n{str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
with gr.Blocks() as app:
|
72 |
+
gr.Markdown("## 🔐 Secure AutoAWQ Quantizer")
|
|
|
73 |
|
74 |
with gr.Row():
|
75 |
+
model_id = gr.Textbox(label="Model ID",
|
76 |
+
placeholder="meta-llama/Meta-Llama-3-8B-Instruct",
|
77 |
+
info="Must have access rights")
|
78 |
+
hf_token = gr.Textbox(label="HF Token",
|
79 |
+
type="password",
|
80 |
+
info="Required for gated models")
|
81 |
+
repo_name = gr.Textbox(label="Destination Repo",
|
82 |
+
info="Format: username/repo-name")
|
83 |
|
84 |
+
go_btn = gr.Button("Start Quantization", variant="primary")
|
85 |
+
output = gr.Markdown()
|
86 |
|
87 |
+
go_btn.click(
|
88 |
+
quantize_model,
|
89 |
+
inputs=[model_id, hf_token, repo_name],
|
90 |
+
outputs=output
|
|
|
|
|
|
|
91 |
)
|
92 |
|
93 |
+
app.launch()
|