Spaces:
Building
Building
Upload 4 files
Browse files
app.py
CHANGED
@@ -13,10 +13,8 @@ import time
|
|
13 |
import requests
|
14 |
from collections import deque
|
15 |
import random
|
16 |
-
import codecs
|
17 |
-
import json
|
18 |
from dataclasses import dataclass
|
19 |
-
from typing import Optional,
|
20 |
|
21 |
app = Flask(__name__)
|
22 |
|
@@ -86,33 +84,44 @@ class GeneratedText:
|
|
86 |
text: str
|
87 |
finish_reason: Optional[str] = None
|
88 |
|
89 |
-
|
90 |
class ResponseWrapper:
|
91 |
def __init__(self, data: Dict[Any, Any]):
|
92 |
self._data = data
|
|
|
93 |
self._text = self._extract_text()
|
94 |
self._finish_reason = self._extract_finish_reason()
|
|
|
|
|
95 |
|
96 |
def _extract_text(self) -> str:
|
97 |
try:
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
100 |
return ""
|
101 |
|
102 |
def _extract_finish_reason(self) -> Optional[str]:
|
103 |
try:
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
106 |
return None
|
107 |
|
108 |
@property
|
109 |
def text(self) -> str:
|
|
|
110 |
return self._text
|
111 |
|
112 |
@property
|
113 |
def finish_reason(self) -> Optional[str]:
|
|
|
114 |
return self._finish_reason
|
115 |
|
|
|
116 |
class APIKeyManager:
|
117 |
def __init__(self):
|
118 |
self.api_keys = re.findall(r"AIzaSy[a-zA-Z0-9_-]{33}", os.environ.get('KeyArray'))
|
@@ -485,7 +494,6 @@ def chat_completions():
|
|
485 |
else:
|
486 |
try:
|
487 |
text_content = response.text
|
488 |
-
logger.info(response)
|
489 |
except (AttributeError, IndexError, TypeError, ValueError) as e:
|
490 |
if "response.candidates" in str(e) or "response.text" in str(e):
|
491 |
logger.error(f"用户输入被AI安全过滤器阻止")
|
|
|
13 |
import requests
|
14 |
from collections import deque
|
15 |
import random
|
|
|
|
|
16 |
from dataclasses import dataclass
|
17 |
+
from typing import Optional, Dict, Any
|
18 |
|
19 |
app = Flask(__name__)
|
20 |
|
|
|
84 |
text: str
|
85 |
finish_reason: Optional[str] = None
|
86 |
|
|
|
87 |
class ResponseWrapper:
|
88 |
def __init__(self, data: Dict[Any, Any]):
|
89 |
self._data = data
|
90 |
+
logger.debug("Initializing ResponseWrapper with data: %s", self._data)
|
91 |
self._text = self._extract_text()
|
92 |
self._finish_reason = self._extract_finish_reason()
|
93 |
+
logger.debug("Text extracted: %s", self._text)
|
94 |
+
logger.debug("Finish reason extracted: %s", self._finish_reason)
|
95 |
|
96 |
def _extract_text(self) -> str:
|
97 |
try:
|
98 |
+
text = self._data['candidates'][0]['content']['parts'][0]['text']
|
99 |
+
logger.debug("Successfully extracted text: %s", text)
|
100 |
+
return text
|
101 |
+
except (KeyError, IndexError) as e:
|
102 |
+
logger.error("Failed to extract text: %s", e)
|
103 |
return ""
|
104 |
|
105 |
def _extract_finish_reason(self) -> Optional[str]:
|
106 |
try:
|
107 |
+
finish_reason = self._data['candidates'][0].get('finishReason')
|
108 |
+
logger.debug("Successfully extracted finish reason: %s", finish_reason)
|
109 |
+
return finish_reason
|
110 |
+
except (KeyError, IndexError) as e:
|
111 |
+
logger.error("Failed to extract finish reason: %s", e)
|
112 |
return None
|
113 |
|
114 |
@property
|
115 |
def text(self) -> str:
|
116 |
+
logger.debug("Accessing text property: %s", self._text)
|
117 |
return self._text
|
118 |
|
119 |
@property
|
120 |
def finish_reason(self) -> Optional[str]:
|
121 |
+
logger.debug("Accessing finish_reason property: %s", self._finish_reason)
|
122 |
return self._finish_reason
|
123 |
|
124 |
+
|
125 |
class APIKeyManager:
|
126 |
def __init__(self):
|
127 |
self.api_keys = re.findall(r"AIzaSy[a-zA-Z0-9_-]{33}", os.environ.get('KeyArray'))
|
|
|
494 |
else:
|
495 |
try:
|
496 |
text_content = response.text
|
|
|
497 |
except (AttributeError, IndexError, TypeError, ValueError) as e:
|
498 |
if "response.candidates" in str(e) or "response.text" in str(e):
|
499 |
logger.error(f"用户输入被AI安全过滤器阻止")
|