Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -11,6 +11,8 @@ import socket
|
|
| 11 |
import time
|
| 12 |
from enum import Enum
|
| 13 |
import random
|
|
|
|
|
|
|
| 14 |
|
| 15 |
#--------------------------------------------------- Definizione Server FAST API ------------------------------------------------------
|
| 16 |
app = FastAPI()
|
|
@@ -31,12 +33,20 @@ class InputData(BaseModel):
|
|
| 31 |
top_p: float = 0.95
|
| 32 |
repetition_penalty: float = 1.0
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
class PostSpazio(BaseModel):
|
| 35 |
nomeSpazio: str
|
| 36 |
input: str = ''
|
| 37 |
api_name: str = "/chat"
|
| 38 |
|
| 39 |
-
|
| 40 |
#--------------------------------------------------- Generazione TESTO ------------------------------------------------------
|
| 41 |
@app.post("/Genera")
|
| 42 |
def read_root(request: Request, input_data: InputData):
|
|
@@ -74,6 +84,37 @@ def format_prompt(message, history):
|
|
| 74 |
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
|
| 75 |
prompt += f"[{now}] [INST] {message} [/INST]"
|
| 76 |
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
#--------------------------------------------------- Generazione IMMAGINE ------------------------------------------------------
|
| 79 |
style_image = {
|
|
|
|
| 11 |
import time
|
| 12 |
from enum import Enum
|
| 13 |
import random
|
| 14 |
+
import aiohttp
|
| 15 |
+
import asyncio
|
| 16 |
|
| 17 |
#--------------------------------------------------- Definizione Server FAST API ------------------------------------------------------
|
| 18 |
app = FastAPI()
|
|
|
|
| 33 |
top_p: float = 0.95
|
| 34 |
repetition_penalty: float = 1.0
|
| 35 |
|
| 36 |
+
class InputDataAsync(BaseModel):
|
| 37 |
+
input: str
|
| 38 |
+
temperature: float = 0.2
|
| 39 |
+
max_new_tokens: int = 30000
|
| 40 |
+
top_p: float = 0.95
|
| 41 |
+
repetition_penalty: float = 1.0
|
| 42 |
+
NumeroGenerazioni: int = 1
|
| 43 |
+
|
| 44 |
class PostSpazio(BaseModel):
|
| 45 |
nomeSpazio: str
|
| 46 |
input: str = ''
|
| 47 |
api_name: str = "/chat"
|
| 48 |
|
| 49 |
+
|
| 50 |
#--------------------------------------------------- Generazione TESTO ------------------------------------------------------
|
| 51 |
@app.post("/Genera")
|
| 52 |
def read_root(request: Request, input_data: InputData):
|
|
|
|
| 84 |
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
|
| 85 |
prompt += f"[{now}] [INST] {message} [/INST]"
|
| 86 |
return prompt
|
| 87 |
+
|
| 88 |
+
#--------------------------------------------------- Generazione TESTO ASYNC ------------------------------------------------------
|
| 89 |
+
@app.post("/GeneraAsync")
|
| 90 |
+
def read_rootAsync(request: Request, input_data: InputDataAsync):
|
| 91 |
+
print(input_data.input)
|
| 92 |
+
data = {
|
| 93 |
+
'input': input_data.input,
|
| 94 |
+
'temperature': input_data.temperature,
|
| 95 |
+
'max_new_tokens': input_data.max_new_tokens,
|
| 96 |
+
'top_p': input_data.top_p,
|
| 97 |
+
'repetition_penalty': input_data.repetition_penalty
|
| 98 |
+
}
|
| 99 |
+
result_data = asyncio.run(GeneraTestoAsync("https://matteoscript-fastapi.hf.space/Genera", data, input_data.NumeroGenerazioni))
|
| 100 |
+
return {"response": result_data}
|
| 101 |
+
|
| 102 |
+
#--------------------------------------------------- Chiamata API Asincrona ------------------------------------------------------
|
| 103 |
+
async def make_request(session, token, data, url):
|
| 104 |
+
headers = {
|
| 105 |
+
'Content-Type': 'application/json',
|
| 106 |
+
'Authorization': 'Bearer ' + token
|
| 107 |
+
}
|
| 108 |
+
async with session.post(url, headers=headers, json=data) as response:
|
| 109 |
+
result_data = await response.json()
|
| 110 |
+
print(result_data)
|
| 111 |
+
return result_data
|
| 112 |
+
|
| 113 |
+
async def GeneraTestoAsync(url, data, NumeroGenerazioni):
|
| 114 |
+
token = os.getenv('TOKEN')
|
| 115 |
+
async with aiohttp.ClientSession() as session:
|
| 116 |
+
tasks = [make_request(session, token, data, url) for _ in range(NumeroGenerazioni)]
|
| 117 |
+
return await asyncio.gather(*tasks)
|
| 118 |
|
| 119 |
#--------------------------------------------------- Generazione IMMAGINE ------------------------------------------------------
|
| 120 |
style_image = {
|