Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,58 +10,28 @@ app = FastAPI()
|
|
| 10 |
|
| 11 |
|
| 12 |
class RequestBody(BaseModel):
|
| 13 |
-
|
| 14 |
-
key_body: str
|
| 15 |
-
text: str
|
| 16 |
|
| 17 |
|
| 18 |
@app.post("/api/v1")
|
| 19 |
async def generate_response(request_body: RequestBody):
|
| 20 |
-
|
| 21 |
-
model = request_body.model
|
| 22 |
-
key_true = os.environ['key']
|
| 23 |
-
key_body = request_body.key_body
|
| 24 |
-
if key_body == key_true:
|
| 25 |
-
if model == "gemini":
|
| 26 |
-
key_gemini = os.environ['key_gemini']
|
| 27 |
-
headers = {'Content-Type': 'application/json',}
|
| 28 |
-
params = {'key': key_gemini}
|
| 29 |
-
json_data = {'contents': [{'parts': [{'text': input_text}]}]}
|
| 30 |
-
response = requests.post('https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent',params=params,headers=headers,json=json_data,)
|
| 31 |
-
all_chunk = response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
json_data = {'messages': [{'id': uuid.uuid4().hex,'content': input_text,'role': 'user',},],'id': uuid.uuid4().hex,'previewToken': None,'userId': None,'codeModelMode': True,'agentMode': { 'mode': True, 'id': 'ImageGenerationLV45LJp', 'name': 'Image Generation',},'trendingAgentMode': {},'isMicMode': False,'maxTokens': 1024,'playgroundTopP': None,'playgroundTemperature': None,'isChromeExt': False,'githubToken': None,'clickedAnswer2': False,'clickedAnswer3': False,'clickedForceWebSearch': False,'visitFromDelta': False,'mobileClient': False,'userSelectedModel': None,'validated': '00f37b34-a166-4efb-bce5-1312d87f2f94',}
|
| 50 |
-
response = requests.post('https://www.blackbox.ai/api/chat', headers=headers, json=json_data)
|
| 51 |
-
print(response.text)
|
| 52 |
-
decoded_string = ''.join(char for char in response.text if char != '\x00' and char != '\x08')
|
| 53 |
-
pattern = r'https?://\S+\.(?:jpg|jpeg|png|gif)'
|
| 54 |
-
match2 = re.search(pattern, decoded_string)
|
| 55 |
-
all_chunk = match2.group()
|
| 56 |
-
|
| 57 |
-
if model == 'gemini2':
|
| 58 |
-
key_gemini2 = os.environ['key_gemini2']
|
| 59 |
-
headers = {'Content-Type': 'application/json',}
|
| 60 |
-
params = {'key': key_gemini2}
|
| 61 |
-
json_data = {'contents': [{'parts': [{'text': input_text}]}]}
|
| 62 |
-
response = requests.post('https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent', params=params, headers=headers, json=json_data)
|
| 63 |
-
all_chunk = response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 64 |
|
| 65 |
-
if key_body != key_true:
|
| 66 |
-
all_chunk = "How's the hack going?"
|
| 67 |
return {"response": all_chunk}
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
class RequestBody(BaseModel):
|
| 13 |
+
prompt: str
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
@app.post("/api/v1")
|
| 17 |
async def generate_response(request_body: RequestBody):
|
| 18 |
+
prompt = request_body.prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def is_russian_word(word):
|
| 21 |
+
return all('а' <= char <= 'я' or 'А' <= char <= 'Я' for char in word)
|
| 22 |
+
|
| 23 |
+
def detect_russian_text(text):
|
| 24 |
+
words = re.findall(r'\b\w+\b', text)
|
| 25 |
+
if not words:
|
| 26 |
+
return False
|
| 27 |
+
russian_word_count = sum(1 for word in words if is_russian_word(word))
|
| 28 |
+
russian_ratio = russian_word_count / len(words)
|
| 29 |
+
return russian_ratio > 0.5
|
| 30 |
+
|
| 31 |
+
if detect_russian_text(prompt):
|
| 32 |
+
try:
|
| 33 |
+
all_chunk = GoogleTranslator(source='ru', target='en').translate(prompt)
|
| 34 |
+
except:
|
| 35 |
+
all_chunk = prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|
|
| 37 |
return {"response": all_chunk}
|