Allen Park commited on
Commit
daf3c40
Β·
1 Parent(s): 8a29b8a

add functionality to update google spreadsheet with queries

Browse files
Files changed (2) hide show
  1. app.py +32 -0
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,16 +1,46 @@
1
  import os
2
  import re
3
  import io
 
4
  from typing import List, Tuple, Union
5
  from pathlib import Path
6
  import gradio as gr
7
  import openai
8
  import pymupdf
9
  from docx import Document
 
 
 
10
 
11
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
12
  LEPTON_API_TOKEN = os.environ.get("LEPTON_API_TOKEN", None)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  PROMPT = """
15
  Given the following QUESTION, DOCUMENT and ANSWER you must analyze the provided answer and determine whether it is faithful to the contents of the DOCUMENT. The ANSWER must not offer new information beyond the context provided in the DOCUMENT. The ANSWER also must not contradict information provided in the DOCUMENT. Output your final verdict by strictly following this format: "PASS" if the answer is faithful to the DOCUMENT and "FAIL" if the answer is not faithful to the DOCUMENT. Show your reasoning.
16
 
@@ -179,6 +209,8 @@ def model_call(question, document, answer, client_base_url):
179
  hallucination, reasoning = parse_patronus_lynx_response(response.choices[0].text)
180
  score = "πŸ”΄ FAIL πŸ”΄" if hallucination else "🟒 PASS 🟒"
181
  combined_reasoning = " ".join(reasoning)[1:-1]
 
 
182
  return combined_reasoning, score
183
 
184
  def return_approximate_token_size(text):
 
1
  import os
2
  import re
3
  import io
4
+ import json
5
  from typing import List, Tuple, Union
6
  from pathlib import Path
7
  import gradio as gr
8
  import openai
9
  import pymupdf
10
  from docx import Document
11
+ from google.oauth2.service_account import Credentials
12
+ from googleapiclient.discovery import build
13
+ from googleapiclient.errors import HttpError
14
 
15
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
16
  LEPTON_API_TOKEN = os.environ.get("LEPTON_API_TOKEN", None)
17
 
18
+ # Set up Google Sheets API credentials
19
+ SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
20
+ SERVICE_ACCOUNT_FILE = os.environ.get("GOOGLE_SECRET_AUTH_JSON", None)
21
+ SPREADSHEET_ID = os.environ.get("SPREADSHEET_ID", None)
22
+
23
+ def addrow_googlesheets(model, question, document, answer, reasoning, score):
24
+ try:
25
+ creds = Credentials.from_service_account_info(json.loads(SERVICE_ACCOUNT_FILE), scopes=SCOPES)
26
+ service = build('sheets', 'v4', credentials=creds)
27
+ body = {
28
+ 'values': [[model, question, document, answer, reasoning, score]]
29
+ }
30
+ result = service.spreadsheets().values().append(
31
+ spreadsheetId=SPREADSHEET_ID,
32
+ range='Sheet1!A:F',
33
+ valueInputOption='RAW',
34
+ insertDataOption='INSERT_ROWS',
35
+ body=body
36
+ ).execute()
37
+ # print(f"{result.get('updates').get('updatedCells')} cells appended.")
38
+ return True
39
+
40
+ except HttpError as error:
41
+ print(f"An error occurred: {error}")
42
+ return False
43
+
44
  PROMPT = """
45
  Given the following QUESTION, DOCUMENT and ANSWER you must analyze the provided answer and determine whether it is faithful to the contents of the DOCUMENT. The ANSWER must not offer new information beyond the context provided in the DOCUMENT. The ANSWER also must not contradict information provided in the DOCUMENT. Output your final verdict by strictly following this format: "PASS" if the answer is faithful to the DOCUMENT and "FAIL" if the answer is not faithful to the DOCUMENT. Show your reasoning.
46
 
 
209
  hallucination, reasoning = parse_patronus_lynx_response(response.choices[0].text)
210
  score = "πŸ”΄ FAIL πŸ”΄" if hallucination else "🟒 PASS 🟒"
211
  combined_reasoning = " ".join(reasoning)[1:-1]
212
+ model = "Patronus Lynx 8B v1.1" if client_base_url=="https://yb15a7dy-lynx-v1-1-8b.tin.lepton.run/api/v1/" else "Patronus Lynx 70B"
213
+ addrow_googlesheets(model, question, document, answer, combined_reasoning, score)
214
  return combined_reasoning, score
215
 
216
  def return_approximate_token_size(text):
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  openai
2
  PyMuPDF
3
- python-docx
 
 
 
1
  openai
2
  PyMuPDF
3
+ python-docx
4
+ google-auth
5
+ google-api-python-client