Spaces:
Sleeping
Sleeping
| 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 | |
| #--------------------------------------------------- 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 InputImage(BaseModel): | |
| input: str | |
| negativePrompt: str = '' | |
| steps: int = 25 | |
| cfg: int = 7 | |
| seed: int = -1 | |
| #--------------------------------------------------- Generazione TESTO ------------------------------------------------------ | |
| 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=42, | |
| ) | |
| 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 IMMAGINE ------------------------------------------------------ | |
| def generate_image(request: Request, input_data: InputImage): | |
| client = Client("https://openskyml-fast-sdxl-stable-diffusion-xl.hf.space/--replicas/545b5tw7n/") | |
| max_attempts = 20 | |
| attempt = 0 | |
| while attempt < max_attempts: | |
| try: | |
| result = client.predict( | |
| input_data.input, | |
| input_data.negativePrompt, | |
| input_data.steps, | |
| input_data.cfg, | |
| 1024, | |
| 1024, | |
| input_data.seed, | |
| fn_index=0 | |
| ) | |
| image_url = result | |
| 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"} | |
| def read_general(): | |
| return {"response": "Benvenuto. Per maggiori info: https://matteoscript-fastapi.hf.space/docs"} | |