from fastapi import FastAPI, Request from pydantic import BaseModel import requests import os import uuid import re app = FastAPI() class RequestBody(BaseModel): model: str key_body: str text: str @app.post("/api/v1") async def generate_response(request_body: RequestBody): input_text = request_body.text model = request_body.model key_true = os.environ['key'] key_body = request_body.key_body if key_body == key_true: if model == "gemini": key_gemini = os.environ['key_gemini'] headers = {'Content-Type': 'application/json',} params = {'key': key_gemini} json_data = {'contents': [{'parts': [{'text': input_text}]}]} response = requests.post('https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent',params=params,headers=headers,json=json_data,) all_chunk = response.json()['candidates'][0]['content']['parts'][0]['text'] if model == 'groq': key_groq = os.environ['key_groq'] headers = {'Authorization': f'Bearer {key_groq}','Content-Type': 'application/json'} json_data = {'messages': [{'role': 'user','content': input_text}],'model': 'llama-3.1-70b-versatile',} response = requests.post('https://api.groq.com/openai/v1/chat/completions', headers=headers, json=json_data) all_chunk = response.json()["choices"][0]["message"]["content"] if model == "cohere": key_cohere = os.environ['key_cohere'] headers = {'accept': 'application/json','content-type': 'application/json','Authorization': f'Bearer {key_cohere}',} data = {"model":"command-r-plus","messages":[{ "role": "user", "content": input_text}]} response = requests.post('https://api.cohere.com/v2/chat', headers=headers, json=data) all_chunk = response.json()['message']['content'][0]['text'] if model == 'blackbox': headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36","Accept": "*/*","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate, br","Referer": "https://www.blackbox.ai","Content-Type": "application/json","Origin": "https://www.blackbox.ai","DNT": "1","Sec-GPC": "1","Alt-Used": "www.blackbox.ai","Connection": "keep-alive",} 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',} response = requests.post('https://www.blackbox.ai/api/chat', headers=headers, json=json_data) decoded_string = ''.join(char for char in response.text if char != '\x00' and char != '\x08') pattern = r'https?://\S+\.(?:jpg|jpeg|png|gif)' match2 = re.search(pattern, decoded_string) all_chunk = match2.group() if model == 'gemini2': key_gemini2 = os.environ['key_gemini2'] headers = {'Content-Type': 'application/json',} params = {'key': key_gemini2} json_data = {'contents': [{'parts': [{'text': input_text}]}]} response = requests.post('https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent', params=params, headers=headers, json=json_data) all_chunk = response.json()['candidates'][0]['content']['parts'][0]['text'] if key_body != key_true: all_chunk = "How's the hack going?" return {"response": all_chunk}