eagle0504 commited on
Commit
dc597fc
β€’
1 Parent(s): a68dd63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -3
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
- from typing import Dict, List, Union
 
3
 
4
  import numpy as np
5
  import openai
@@ -58,6 +59,65 @@ def ai_judge(prompt: str) -> float:
58
  return call_chatgpt(prompt)
59
 
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  ## rag strategy 1
62
  # file_names = [f"output_files/file_{i}.txt" for i in range(131)]
63
  # # file_names = [f"output_files_large/file_{i}.txt" for i in range(1310)]
@@ -161,7 +221,7 @@ if prompt := st.chat_input("Tell me about YSA"):
161
  ref = pd.DataFrame(
162
  {
163
  "idx": idx,
164
- "question": [dataset["train"]['questions'][i] for i in idx],
165
  "answers": [dataset["train"]['answers'][i] for i in idx],
166
  "distances": results["distances"][0]
167
  }
@@ -177,6 +237,20 @@ if prompt := st.chat_input("Tell me about YSA"):
177
  ref_from_db_search = ref["answers"]
178
  final_ref = ref
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  independent_ai_judge_score = []
181
  for i in range(final_ref.shape[0]):
182
  this_quest = question
@@ -188,7 +262,7 @@ if prompt := st.chat_input("Tell me about YSA"):
188
 
189
  From 0 to 10, rate how well the content address the user's question.
190
 
191
- Only produce a number from 0 to 10.
192
  """
193
  this_score = ai_judge(prompt_for_ai_judge)
194
  independent_ai_judge_score.append(this_score)
 
1
  import os
2
+ from typing import List, Tuple, Dict, Union, Any
3
+ import requests
4
 
5
  import numpy as np
6
  import openai
 
59
  return call_chatgpt(prompt)
60
 
61
 
62
+ def query(payload: Dict[str, Any]) -> Dict[str, Any]:
63
+ """
64
+ Sends a JSON payload to a predefined API URL and returns the JSON response.
65
+ Args:
66
+ payload (Dict[str, Any]): The JSON payload to be sent to the API.
67
+ Returns:
68
+ Dict[str, Any]: The JSON response received from the API.
69
+ """
70
+
71
+ # API endpoint URL
72
+ API_URL = "https://sks7h7h5qkhoxwxo.us-east-1.aws.endpoints.huggingface.cloud"
73
+
74
+ # Headers to indicate both the request and response formats are JSON
75
+ headers = {
76
+ "Accept": "application/json",
77
+ "Content-Type": "application/json"
78
+ }
79
+
80
+ # Sending a POST request with the JSON payload and headers
81
+ response = requests.post(API_URL, headers=headers, json=payload)
82
+
83
+ # Returning the JSON response
84
+ return response.json()
85
+
86
+
87
+ def llama2_7b_ysa(prompt: str) -> str:
88
+ """
89
+ Queries a model and retrieves the generated text based on the given prompt.
90
+ This function sends a prompt to a model (presumably named 'llama2_7b') and extracts
91
+ the generated text from the model's response. It's tailored for handling responses
92
+ from a specific API or model query structure where the response is expected to be
93
+ a list of dictionaries, with at least one dictionary containing a key 'generated_text'.
94
+ Parameters:
95
+ - prompt (str): The text prompt to send to the model.
96
+ Returns:
97
+ - str: The generated text response from the model.
98
+ Note:
99
+ - The function assumes that the 'query' function is previously defined and accessible
100
+ within the same scope or module. It should send a request to the model and return
101
+ the response in a structured format.
102
+ - The 'parameters' dictionary is passed empty but can be customized to include specific
103
+ request parameters as needed by the model API.
104
+ """
105
+
106
+ # Define the query payload with the prompt and any additional parameters
107
+ query_payload: Dict[str, Any] = {
108
+ "inputs": prompt,
109
+ "parameters": {}
110
+ }
111
+
112
+ # Send the query to the model and store the output response
113
+ output = query(query_payload)
114
+
115
+ # Extract the 'generated_text' from the first item in the response list
116
+ response: str = output[0]['generated_text']
117
+
118
+ return response
119
+
120
+
121
  ## rag strategy 1
122
  # file_names = [f"output_files/file_{i}.txt" for i in range(131)]
123
  # # file_names = [f"output_files_large/file_{i}.txt" for i in range(1310)]
 
221
  ref = pd.DataFrame(
222
  {
223
  "idx": idx,
224
+ "questions": [dataset["train"]['questions'][i] for i in idx],
225
  "answers": [dataset["train"]['answers'][i] for i in idx],
226
  "distances": results["distances"][0]
227
  }
 
237
  ref_from_db_search = ref["answers"]
238
  final_ref = ref
239
 
240
+ try:
241
+ llm_response = llama2_7b_brk_letters(question)
242
+ except:
243
+ llm_response = "Sorry, the inference endpoint is temporarily down. πŸ˜”"
244
+
245
+ finetuned_llm_guess = {
246
+ "idx": "from_llm",
247
+ "questions": question,
248
+ "answers": llm_response,
249
+ "distances": 0
250
+ }
251
+ final_ref = final_ref.append(finetuned_llm_guess)
252
+
253
+ # add ai judge as additional rating
254
  independent_ai_judge_score = []
255
  for i in range(final_ref.shape[0]):
256
  this_quest = question
 
262
 
263
  From 0 to 10, rate how well the content address the user's question.
264
 
265
+ Only produce a number from 0 to 10 while 10 being the best at addressing user's question.
266
  """
267
  this_score = ai_judge(prompt_for_ai_judge)
268
  independent_ai_judge_score.append(this_score)