aashish1904 commited on
Commit
2d13f10
1 Parent(s): 652c7fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -62
app.py CHANGED
@@ -1,63 +1,95 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()