Spaces:
Build error
Build error
Update main.py
Browse files10 unmodified lines111213141516171814 unmodified lines33343536373839404142434445464748394950515231 unmodified lines8485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912010 unmodified linesimport timefrom enum import Enumimport randomimport aiohttpimport asyncio
#--------------------------------------------------- Definizione Server FAST API ------------------------------------------------------app = FastAPI()14 unmodified lines top_p: float = 0.95 repetition_penalty: float = 1.0
class InputDataAsync(BaseModel): input: str temperature: float = 0.2 max_new_tokens: int = 30000 top_p: float = 0.95 repetition_penalty: float = 1.0 NumeroGenerazioni: int = 1 class PostSpazio(BaseModel): nomeSpazio: str input: str = '' api_name: str = "/chat"
#--------------------------------------------------- Generazione TESTO ------------------------------------------------------@app.post("/Genera")def read_root(request: Request, input_data: InputData):31 unmodified lines now = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") prompt += f"[{now}] [INST] {message} [/INST]" return prompt
#--------------------------------------------------- Generazione TESTO ASYNC ------------------------------------------------------@app.post("/GeneraAsync")def read_rootAsync(request: Request, input_data: InputDataAsync): print(input_data.input) data = { 'input': input_data.input, 'temperature': input_data.temperature, 'max_new_tokens': input_data.max_new_tokens, 'top_p': input_data.top_p, 'repetition_penalty': input_data.repetition_penalty } result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", data, input_data.NumeroGenerazioni)) return {"response": result_data}
#--------------------------------------------------- Chiamata API Asincrona ------------------------------------------------------async def make_request(session, token, data, url): headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } async with session.post(url, headers=headers, json=data) as response: result_data = await response.json() print(result_data) return result_data
async def GeneraTestoAsync(url, data, NumeroGenerazioni): token = os.getenv('TOKEN') async with aiohttp.ClientSession() as session: tasks = [make_request(session, token, data, url) for _ in range(NumeroGenerazioni)] return await asyncio.gather(*tasks) #--------------------------------------------------- Generazione IMMAGINE ------------------------------------------------------style_image = {main.py+42-1