Spaces:
Running
Running
aashish1904
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,95 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import gspread
|
3 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import requests
|
7 |
+
|
8 |
+
# Load Google Sheets credentials from secrets
|
9 |
+
creds_json = os.getenv("GOOGLE_SHEETS_KEY_JSON")
|
10 |
+
creds_dict = json.loads(creds_json)
|
11 |
+
|
12 |
+
# Google Sheets setup
|
13 |
+
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
14 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
15 |
+
client = gspread.authorize(creds)
|
16 |
+
sheet = client.open("QF - Quant Request").sheet1 # Use the correct sheet name
|
17 |
+
|
18 |
+
def validate_repo(link):
|
19 |
+
"""Check if the repository exists on Hugging Face."""
|
20 |
+
# Extract repo_id from the link (format: {username}/{model_name} or {dataset_name})
|
21 |
+
repo_id = link.split("https://huggingface.co/")[-1].strip("/")
|
22 |
+
url = f"https://huggingface.co/api/models/{repo_id}" # Adjust the URL for datasets if needed
|
23 |
+
|
24 |
+
response = requests.head(url)
|
25 |
+
return response.status_code == 200
|
26 |
+
|
27 |
+
def check_quant_exists(link):
|
28 |
+
"""Check if the quantization request already exists in the Google Sheet."""
|
29 |
+
extracted_text = link.split("https://huggingface.co/")[-1]
|
30 |
+
existing_texts = sheet.col_values(1) # Assuming the extracted text is in the first column
|
31 |
+
return extracted_text in existing_texts
|
32 |
+
|
33 |
+
def check_quant_factory_quant_exists(link):
|
34 |
+
"""Check if a GGUF quantized model exists in the QuantFactory repository."""
|
35 |
+
# Extract the model name from the original link
|
36 |
+
model_name = link.split('/')[-1] # Get the last item from the split list, which is the model name
|
37 |
+
|
38 |
+
# Create the QuantFactory GGUF quant link
|
39 |
+
quant_factory_link = f"https://huggingface.co/QuantFactory/{model_name}-GGUF"
|
40 |
+
|
41 |
+
# Check if the QuantFactory GGUF repo exists
|
42 |
+
response = requests.head(quant_factory_link)
|
43 |
+
|
44 |
+
if response.status_code == 200:
|
45 |
+
return True, quant_factory_link
|
46 |
+
else:
|
47 |
+
return False, quant_factory_link
|
48 |
+
|
49 |
+
def submit_link(link):
|
50 |
+
# Normalize the input link
|
51 |
+
if not link.startswith("https://huggingface.co"):
|
52 |
+
link = f"https://huggingface.co/{link}"
|
53 |
+
|
54 |
+
# Validate the repo
|
55 |
+
if not validate_repo(link):
|
56 |
+
return "Invalid model or repository link. Please provide a valid Hugging Face link."
|
57 |
+
|
58 |
+
# Check if the GGUF quantized model already exists in QuantFactory
|
59 |
+
quant_exists, quant_link = check_quant_factory_quant_exists(link)
|
60 |
+
if quant_exists:
|
61 |
+
return f"Quant already exists at [QuantFactory/{link.split('/')[-1]}-GGUF]({quant_link})."
|
62 |
+
|
63 |
+
# Check if the quant request already exists in the Google Sheet
|
64 |
+
if check_quant_exists(link):
|
65 |
+
return "Quant requests have already been made for this model."
|
66 |
+
|
67 |
+
# Extract text after "huggingface.co/"
|
68 |
+
extracted_text = link.split("https://huggingface.co/")[-1]
|
69 |
+
|
70 |
+
# Append the row with the extracted text and default status "Pending"
|
71 |
+
row_index = len(sheet.get_all_values()) + 1
|
72 |
+
sheet.append_row(["", ""]) # Append an empty row first to ensure correct row index
|
73 |
+
|
74 |
+
# Set the hyperlink formula in the first column
|
75 |
+
sheet.update_cell(row_index, 1, f'=HYPERLINK("{link}", "{extracted_text}")')
|
76 |
+
|
77 |
+
# Copy content from cell B2 to the new row in column B
|
78 |
+
b2_value = sheet.cell(2, 2).value
|
79 |
+
sheet.update_cell(row_index, 2, b2_value)
|
80 |
+
|
81 |
+
return "Request submitted successfully."
|
82 |
+
|
83 |
+
# Gradio Interface
|
84 |
+
with gr.Blocks() as demo:
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column(scale=1): # Left column
|
87 |
+
gr.Markdown("## QuantFactory - Model Request")
|
88 |
+
link_input = gr.Textbox(label="Hugging Face Link")
|
89 |
+
submit_button = gr.Button("Submit")
|
90 |
+
result = gr.Textbox(label="Result", interactive=False)
|
91 |
+
submit_button.click(fn=submit_link, inputs=link_input, outputs=result)
|
92 |
+
with gr.Column(scale=1): # Right column
|
93 |
+
gr.Image("image.png") # Display the image on the right
|
94 |
+
|
95 |
+
demo.launch()
|