Spaces:
Paused
Paused
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -90,10 +90,16 @@ class ResponseWrapper:
|
|
| 90 |
self._data = data
|
| 91 |
self._text = self._extract_text()
|
| 92 |
self._finish_reason = self._extract_finish_reason()
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def _extract_text(self) -> str:
|
| 95 |
try:
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
except (KeyError, IndexError):
|
| 98 |
return ""
|
| 99 |
|
|
@@ -103,6 +109,24 @@ class ResponseWrapper:
|
|
| 103 |
except (KeyError, IndexError):
|
| 104 |
return None
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
@property
|
| 107 |
def text(self) -> str:
|
| 108 |
return self._text
|
|
@@ -111,6 +135,18 @@ class ResponseWrapper:
|
|
| 111 |
def finish_reason(self) -> Optional[str]:
|
| 112 |
return self._finish_reason
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
class APIKeyManager:
|
| 115 |
def __init__(self):
|
| 116 |
self.api_keys = re.findall(r"AIzaSy[a-zA-Z0-9_-]{33}", os.environ.get('KeyArray'))
|
|
|
|
| 90 |
self._data = data
|
| 91 |
self._text = self._extract_text()
|
| 92 |
self._finish_reason = self._extract_finish_reason()
|
| 93 |
+
self._prompt_token_count = self._extract_prompt_token_count()
|
| 94 |
+
self._candidates_token_count = self._extract_candidates_token_count()
|
| 95 |
+
self._total_token_count = self._extract_total_token_count()
|
| 96 |
|
| 97 |
def _extract_text(self) -> str:
|
| 98 |
try:
|
| 99 |
+
for part in self._data['candidates'][0]['content']['parts']:
|
| 100 |
+
if part.get('role') == 'model':
|
| 101 |
+
return part['text']
|
| 102 |
+
return ""
|
| 103 |
except (KeyError, IndexError):
|
| 104 |
return ""
|
| 105 |
|
|
|
|
| 109 |
except (KeyError, IndexError):
|
| 110 |
return None
|
| 111 |
|
| 112 |
+
def _extract_prompt_token_count(self) -> Optional[int]:
|
| 113 |
+
try:
|
| 114 |
+
return self._data['usageMetadata'].get('promptTokenCount')
|
| 115 |
+
except (KeyError):
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
def _extract_candidates_token_count(self) -> Optional[int]:
|
| 119 |
+
try:
|
| 120 |
+
return self._data['usageMetadata'].get('candidatesTokenCount')
|
| 121 |
+
except (KeyError):
|
| 122 |
+
return None
|
| 123 |
+
|
| 124 |
+
def _extract_total_token_count(self) -> Optional[int]:
|
| 125 |
+
try:
|
| 126 |
+
return self._data['usageMetadata'].get('totalTokenCount')
|
| 127 |
+
except (KeyError):
|
| 128 |
+
return None
|
| 129 |
+
|
| 130 |
@property
|
| 131 |
def text(self) -> str:
|
| 132 |
return self._text
|
|
|
|
| 135 |
def finish_reason(self) -> Optional[str]:
|
| 136 |
return self._finish_reason
|
| 137 |
|
| 138 |
+
@property
|
| 139 |
+
def prompt_token_count(self) -> Optional[int]:
|
| 140 |
+
return self._prompt_token_count
|
| 141 |
+
|
| 142 |
+
@property
|
| 143 |
+
def candidates_token_count(self) -> Optional[int]:
|
| 144 |
+
return self._candidates_token_count
|
| 145 |
+
|
| 146 |
+
@property
|
| 147 |
+
def total_token_count(self) -> Optional[int]:
|
| 148 |
+
return self._total_token_count
|
| 149 |
+
|
| 150 |
class APIKeyManager:
|
| 151 |
def __init__(self):
|
| 152 |
self.api_keys = re.findall(r"AIzaSy[a-zA-Z0-9_-]{33}", os.environ.get('KeyArray'))
|