richardr1126 commited on
Commit
664c783
β€’
1 Parent(s): de09581

Add firebase logging

Browse files
Files changed (3) hide show
  1. .gitignore +1 -1
  2. app-ngrok.py +46 -3
  3. requirements.txt +2 -1
.gitignore CHANGED
@@ -1,4 +1,4 @@
1
  venv/
2
  .venv/
3
  env/
4
- .env/
 
1
  venv/
2
  .venv/
3
  env/
4
+ .env
app-ngrok.py CHANGED
@@ -5,15 +5,49 @@ import requests
5
  from time import sleep
6
  import re
7
  import platform
 
 
 
 
 
8
 
9
  print(f"Running on {platform.system()}")
10
 
 
 
 
 
11
  quantized_model = "richardr1126/spider-skeleton-wizard-coder-ggml"
12
  merged_model = "richardr1126/spider-skeleton-wizard-coder-merged"
13
  initial_model = "WizardLM/WizardCoder-15B-V1.0"
14
  lora_model = "richardr1126/spider-skeleton-wizard-coder-qlora"
15
  dataset = "richardr1126/spider-skeleton-context-instruct"
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def format(text):
18
  # Split the text by "|", and get the last element in the list which should be the final query
19
  try:
@@ -33,7 +67,9 @@ def format(text):
33
 
34
  return final_query_markdown
35
 
36
- def generate(input_message: str, db_info="", temperature=0.1, top_p=0.9, top_k=0, repetition_penalty=1.08, format_sql=True, stop_sequence="###"):
 
 
37
  # Format the user's input message
38
  messages = f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n\nConvert text to sql: {input_message} {db_info}\n\n### Response:\n\n"
39
 
@@ -66,10 +102,15 @@ def generate(input_message: str, db_info="", temperature=0.1, top_p=0.9, top_k=0
66
  if response_text and response_text[-1] == ".":
67
  response_text = response_text[:-1]
68
 
 
 
 
 
69
  if format_sql:
70
- return format(response_text)
71
  else:
72
  return response_text
 
73
 
74
  except Exception as e:
75
  print(f'Error occurred: {str(e)}')
@@ -102,10 +143,12 @@ with gr.Blocks(theme='gradio/soft') as demo:
102
  # When the button is clicked, call the generate function, inputs are taken from the UI elements, outputs are sent to outputs elements
103
  run_button.click(fn=generate, inputs=[input_text, db_info, temperature, top_p, top_k, repetition_penalty, format_sql, stop_sequence], outputs=output_box, api_name="txt2sql")
104
 
 
105
  info = gr.HTML(f"""
106
  <p>🌐 Leveraging the <a href='https://huggingface.co/{quantized_model}'><strong>4-bit GGML version</strong></a> of <a href='https://huggingface.co/{merged_model}'><strong>{merged_model}</strong></a> model.</p>
107
  <p>πŸ”— How it's made: <a href='https://huggingface.co/{initial_model}'><strong>{initial_model}</strong></a> was finetuned to create <a href='https://huggingface.co/{lora_model}'><strong>{lora_model}</strong></a>, then merged together to create <a href='https://huggingface.co/{merged_model}'><strong>{merged_model}</strong></a>.</p>
108
  <p>πŸ“‰ Fine-tuning was performed using QLoRA techniques on the <a href='https://huggingface.co/datasets/{dataset}'><strong>{dataset}</strong></a> dataset. You can view training metrics on the <a href='https://huggingface.co/{lora_model}'><strong>QLoRa adapter HF Repo</strong></a>.</p>
 
109
  """)
110
 
111
  with gr.Accordion("Examples", open=True):
@@ -135,4 +178,4 @@ with gr.Blocks(theme='gradio/soft') as demo:
135
  readme_content,
136
  )
137
 
138
- demo.queue(concurrency_count=1, max_size=10).launch(debug=True)
 
5
  from time import sleep
6
  import re
7
  import platform
8
+ # Additional Firebase imports
9
+ import firebase_admin
10
+ from firebase_admin import credentials, firestore
11
+ import json
12
+ import base64
13
 
14
  print(f"Running on {platform.system()}")
15
 
16
+ if platform.system() == "Windows" or platform.system() == "Darwin":
17
+ from dotenv import load_dotenv
18
+ load_dotenv()
19
+
20
  quantized_model = "richardr1126/spider-skeleton-wizard-coder-ggml"
21
  merged_model = "richardr1126/spider-skeleton-wizard-coder-merged"
22
  initial_model = "WizardLM/WizardCoder-15B-V1.0"
23
  lora_model = "richardr1126/spider-skeleton-wizard-coder-qlora"
24
  dataset = "richardr1126/spider-skeleton-context-instruct"
25
 
26
+ def log_to_firestore(input_message, db_info, temperature, response_text):
27
+ # For firebase
28
+ base64_string = os.getenv('FIREBASE')
29
+ base64_bytes = base64_string.encode('utf-8')
30
+ json_bytes = base64.b64decode(base64_bytes)
31
+ json_data = json_bytes.decode('utf-8')
32
+
33
+ firebase_auth = json.loads(json_data)
34
+
35
+ # Load credentials and initialize Firestore
36
+ cred = credentials.Certificate(firebase_auth)
37
+ firebase_admin.initialize_app(cred)
38
+ db = firestore.client()
39
+
40
+ doc_ref = db.collection('logs').document()
41
+ log_data = {
42
+ 'timestamp': firestore.SERVER_TIMESTAMP,
43
+ 'temperature': temperature,
44
+ 'db_info': db_info,
45
+ 'input': input_message,
46
+ 'output': response_text
47
+ }
48
+ doc_ref.set(log_data)
49
+
50
+
51
  def format(text):
52
  # Split the text by "|", and get the last element in the list which should be the final query
53
  try:
 
67
 
68
  return final_query_markdown
69
 
70
+
71
+
72
+ def generate(input_message: str, db_info="", temperature=0.1, top_p=0.9, top_k=0, repetition_penalty=1.08, format_sql=True, stop_sequence="###", log=False):
73
  # Format the user's input message
74
  messages = f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n\nConvert text to sql: {input_message} {db_info}\n\n### Response:\n\n"
75
 
 
102
  if response_text and response_text[-1] == ".":
103
  response_text = response_text[:-1]
104
 
105
+ # Log the request to Firestore
106
+ formatted_query = format(response_text)
107
+ log_to_firestore(input_message, db_info, temperature, formatted_query if format_sql else response_text)
108
+
109
  if format_sql:
110
+ return formatted_query
111
  else:
112
  return response_text
113
+
114
 
115
  except Exception as e:
116
  print(f'Error occurred: {str(e)}')
 
143
  # When the button is clicked, call the generate function, inputs are taken from the UI elements, outputs are sent to outputs elements
144
  run_button.click(fn=generate, inputs=[input_text, db_info, temperature, top_p, top_k, repetition_penalty, format_sql, stop_sequence], outputs=output_box, api_name="txt2sql")
145
 
146
+ ## Add statement saying that inputs/outpus are sent to firebase
147
  info = gr.HTML(f"""
148
  <p>🌐 Leveraging the <a href='https://huggingface.co/{quantized_model}'><strong>4-bit GGML version</strong></a> of <a href='https://huggingface.co/{merged_model}'><strong>{merged_model}</strong></a> model.</p>
149
  <p>πŸ”— How it's made: <a href='https://huggingface.co/{initial_model}'><strong>{initial_model}</strong></a> was finetuned to create <a href='https://huggingface.co/{lora_model}'><strong>{lora_model}</strong></a>, then merged together to create <a href='https://huggingface.co/{merged_model}'><strong>{merged_model}</strong></a>.</p>
150
  <p>πŸ“‰ Fine-tuning was performed using QLoRA techniques on the <a href='https://huggingface.co/datasets/{dataset}'><strong>{dataset}</strong></a> dataset. You can view training metrics on the <a href='https://huggingface.co/{lora_model}'><strong>QLoRa adapter HF Repo</strong></a>.</p>
151
+ <p>πŸ“Š All inputs/outputs are logged to Firebase, to help me see where the model still needs improvements.</a>.</p>
152
  """)
153
 
154
  with gr.Accordion("Examples", open=True):
 
178
  readme_content,
179
  )
180
 
181
+ demo.queue(concurrency_count=1, max_size=20).launch(debug=True)
requirements.txt CHANGED
@@ -7,4 +7,5 @@ bitsandbytes
7
  scipy
8
  transformers
9
  accelerate
10
- sqlparse
 
 
7
  scipy
8
  transformers
9
  accelerate
10
+ sqlparse
11
+ firebase_admin