Team Ai
Apppublic

sp12138sp/OpenAPI

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py61 linesDownload Raw Back to root
1from fastapi import FastAPI2from typing import Optional3import requests4import json5from pydantic import BaseModel6 7 8class Item(BaseModel):9    model: str10    messages: list11    temperature: float12    max_tokens: Optional[int] = None13 14 15 16# NOTE - we configure docs_url to serve the interactive Docs at the root path17# of the app. This way, we can use the docs as a landing page for the app on Spaces.18app = FastAPI()19 20API_ENDPOINT = "https://api.openai.com/v1/chat/completions"21 22def reog_file():23    with open('./openapis.txt', 'r+') as file:24        lines = file.readlines()25        lines.append(lines[0])26        lines = lines[1:]27        file.seek(0)28        file.writelines(lines)29        file.close()30 31def read_file():32    with open('./openapis.txt', 'r+') as file:33        lines = file.readlines()34        file.close()35    return lines, len(lines)36 37def generate_chat_completion(data):38    apis, num_apis = read_file()39    api_index = 040    while(1):41        api_key = apis[api_index][:-1]42        print(api_key)43        headers = {44            "Content-Type": "application/json",45            "Authorization": f"Bearer {api_key}",46        }47 48        response = requests.post(API_ENDPOINT, headers=headers, data=data.json())49        if response.status_code == 200:50            return response.json()51        else:52            api_index += 153            reog_file()54            if api_index == num_apis:55                raise Exception("Error")56 57 58@app.post("/generate")59def generate(data: Item):60    output = generate_chat_completion(data)61    return output