Spaces:
Runtime error
Runtime error
File size: 13,975 Bytes
002fca8 2cd7197 9e3ea07 c53513a d707be1 2589dc0 dadb627 0099d95 c5f58d3 f0feabf d57ded5 498d80c 1ad1813 d707be1 c550535 b916cdf c53513a 2cd7197 9e3ea07 0a9fba8 a81da59 0a9fba8 1ad1813 0bb76a8 6159237 a644f61 1ad1813 c550535 a81da59 0a9fba8 a81da59 c550535 a81da59 33e9df8 c53513a 396921a c53513a 609a4fb ca7a52b c550535 1ad1813 c550535 d57ded5 b134cae be4a7bb d57ded5 c550535 1501a26 a9fa60b d57ded5 a9fa60b 498d80c d57ded5 ad9c967 c550535 1e200c7 ad9c967 1501a26 8bcfd35 586a47b c550535 31c3ad2 c550535 31c3ad2 c550535 c53513a 0bb76a8 347ae61 6159237 0bb76a8 6159237 c550535 7a4300a ad9c967 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware # Importa il middleware CORS
from pydantic import BaseModel
from huggingface_hub import InferenceClient
from datetime import datetime
from gradio_client import Client
import base64
import requests
import os
import socket
import time
from enum import Enum
import random
import aiohttp
import asyncio
#--------------------------------------------------- Definizione Server FAST API ------------------------------------------------------
app = FastAPI()
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class InputData(BaseModel):
input: str
temperature: float = 0.2
max_new_tokens: int = 30000
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):
input_text = 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
history = []
generated_response = generate(input_text, history, temperature, max_new_tokens, top_p, repetition_penalty)
return {"response": generated_response}
def generate(prompt, history, temperature=0.2, max_new_tokens=30000, top_p=0.95, repetition_penalty=1.0):
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=random.randint(0, 10**7),
)
formatted_prompt = format_prompt(prompt, history)
output = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=False)
return output
def format_prompt(message, history):
prompt = "<s>"
for user_prompt, bot_response in history:
prompt += f"[INST] {user_prompt} [/INST]"
prompt += f" {bot_response}</s> "
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 = {
"PROFESSIONAL-PHOTO": {
"descrizione": "Professional photo {prompt} . Vivid colors, Mirrorless, 35mm lens, f/1.8 aperture, ISO 100, natural daylight",
"negativePrompt": "out of frame, lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature"
},
"CINEMATIC-PHOTO": {
"descrizione": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
"negativePrompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly"
},
"CINEMATIC-PORTRAIT": {
"descrizione": "cinematic portrait {prompt} 8k, ultra realistic, good vibes, vibrant",
"negativePrompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly"
},
"LINE-ART-DRAWING": {
"descrizione": "line art drawing {prompt} . professional, sleek, modern, minimalist, graphic, line art, vector graphics",
"negativePrompt": "anime, photorealistic, 35mm film, deformed, glitch, blurry, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, mutated, realism, realistic, impressionism, expressionism, oil, acrylic"
},
"COMIC": {
"descrizione": "comic {prompt} . graphic illustration, comic art, graphic novel art, vibrant, highly detailed",
"negativePrompt": "photograph, deformed, glitch, noisy, realistic, stock photo"
},
"ADVERTISING-POSTER-STYLE": {
"descrizione": "advertising poster style {prompt} . Professional, modern, product-focused, commercial, eye-catching, highly detailed",
"negativePrompt": "noisy, blurry, amateurish, sloppy, unattractive"
},
"RETAIL-PACKAGING-STYLE": {
"descrizione": "retail packaging style {prompt} . vibrant, enticing, commercial, product-focused, eye-catching, professional, highly detailed",
"negativePrompt": "noisy, blurry, amateurish, sloppy, unattractive"
},
"GRAFFITI-STYLE": {
"descrizione": "graffiti style {prompt} . street art, vibrant, urban, detailed, tag, mural",
"negativePrompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic"
},
"POP-ART-STYLE": {
"descrizione": "pop Art style {prompt} . bright colors, bold outlines, popular culture themes, ironic or kitsch",
"negativePrompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, minimalist"
},
"ISOMETRIC-STYLE": {
"descrizione": "isometric style {prompt} . vibrant, beautiful, crisp, detailed, ultra detailed, intricate",
"negativePrompt": "deformed, mutated, ugly, disfigured, blur, blurry, noise, noisy, realistic, photographic"
},
"LOW-POLY-STYLE": {
"descrizione": "low-poly style {prompt}. ambient occlusion, low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition",
"negativePrompt": "noisy, sloppy, messy, grainy, highly detailed, ultra textured, photo"
},
"CLAYMATION-STYLE": {
"descrizione": "claymation style {prompt} . sculpture, clay art, centered composition, play-doh",
"negativePrompt": ""
},
"PROFESSIONAL-3D-MODEL": {
"descrizione": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
"negativePrompt": "ugly, deformed, noisy, low poly, blurry, painting"
},
"ANIME-ARTWORK": {
"descrizione": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
"negativePrompt": "photo, deformed, black and white, realism, disfigured, low contrast"
},
"ETHEREAL-FANTASY-CONCEPT-ART": {
"descrizione": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
"negativePrompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white"
},
"CYBERNETIC-STYLE": {
"descrizione": "cybernetic style {prompt} . futuristic, technological, cybernetic enhancements, robotics, artificial intelligence themes",
"negativePrompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, historical, medieval"
},
"FUTURISTIC-STYLE": {
"descrizione": "futuristic style {prompt} . sleek, modern, ultramodern, high tech, detailed",
"negativePrompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, vintage, antique"
},
"SCI-FI-STYLE": {
"descrizione": "sci-fi style {prompt} . futuristic, technological, alien worlds, space themes, advanced civilizations",
"negativePrompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, historical, medieval"
},
"DIGITAL-ART": {
"descrizione": "Digital Art {prompt} . vibrant, cute, digital, handmade",
"negativePrompt": ""
},
"SIMPLE-LOGO": {
"descrizione": "Minimalist Logo {prompt} . material design, primary colors, stylized, minimalist",
"negativePrompt": "3D, high detail, noise, grainy, blurry, painting, drawing, photo, disfigured"
},
"MINIMALISTIC-LOGO": {
"descrizione": "Ultra-minimalist Material Design logo for a BRAND: {prompt} . simple, few colors, clean lines, minimal details, modern color palette, no shadows",
"negativePrompt": "3D, high detail, noise, grainy, blurry, painting, drawing, photo, disfigured"
}
}
class InputImage(BaseModel):
input: str
negativePrompt: str = ''
style: str = ''
steps: int = 25
cfg: int = 6
seed: int = -1
@app.post("/Immagine")
def generate_image(request: Request, input_data: InputImage):
client = Client("https://manjushri-sdxl-1-0.hf.space/")
if input_data.style:
print(input_data.style)
if input_data.style == 'RANDOM':
random_style = random.choice(list(style_image.keys()))
style_info = style_image[random_style]
input_data.input = style_info["descrizione"].format(prompt=input_data.input)
input_data.negativePrompt = style_info["negativePrompt"]
elif input_data.style in style_image:
style_info = style_image[input_data.style]
input_data.input = style_info["descrizione"].format(prompt=input_data.input)
input_data.negativePrompt = style_info["negativePrompt"]
max_attempts = 2
attempt = 0
while attempt < max_attempts:
try:
result = client.predict(
input_data.input, # str in 'What you want the AI to generate. 77 Token Limit. A Token is Any Word, Number, Symbol, or Punctuation. Everything Over 77 Will Be Truncated!' Textbox component
input_data.negativePrompt, # str in 'What you Do Not want the AI to generate. 77 Token Limit' Textbox component
1024, # int | float (numeric value between 512 and 1024) in 'Height' Slider component
1024, # int | float (numeric value between 512 and 1024) in 'Width' Slider component
input_data.cfg, # int | float (numeric value between 1 and 15) in 'Guidance Scale: How Closely the AI follows the Prompt' Slider component
input_data.steps, # int | float (numeric value between 25 and 100) in 'Number of Iterations' Slider component
0, # int | float (numeric value between 0 and 999999999999999999) in 'Seed: 0 is Random' Slider component
"Yes", # str in 'Upscale?' Radio component
"", # str in 'Embedded Prompt' Textbox component
"", # str in 'Embedded Negative Prompt' Textbox component
0.99, # int | float (numeric value between 0.7 and 0.99) in 'Refiner Denoise Start %' Slider component
100, # int | float (numeric value between 1 and 100) in 'Refiner Number of Iterations %' Slider component
api_name="/predict"
)
image_url = result[0]
print(image_url)
with open(image_url, 'rb') as img_file:
img_binary = img_file.read()
img_base64 = base64.b64encode(img_binary).decode('utf-8')
return {"response": img_base64}
except requests.exceptions.HTTPError as e:
time.sleep(1)
attempt += 1
if attempt < max_attempts:
continue
else:
return {"error": "Errore interno del server persistente"}
return {"error": "Numero massimo di tentativi raggiunto"}
#--------------------------------------------------- API PostSpazio ------------------------------------------------------
@app.post("/PostSpazio")
def generate_postspazio(request: Request, input_data: PostSpazio):
client = Client(input_data.nomeSpazio)
result = client.predict(
input_data.input,
api_name=input_data.api_name
)
return {"response": result}
@app.get("/")
def read_general():
return {"response": "Benvenuto. Per maggiori info: https://matteoscript-fastapi.hf.space/docs"} |