Spaces:
Sleeping
Sleeping
File size: 2,072 Bytes
6ee90aa 0551907 6ee90aa 27a4aad e5591d2 6f96f84 b837945 b7c9474 080b041 6ee90aa 03ee1c6 080b041 79e06e3 1b710b0 6ee90aa 03ee1c6 6ee90aa 6f96f84 03ee1c6 79e06e3 03ee1c6 79e06e3 c5c4414 03ee1c6 c5c4414 79e06e3 6ee90aa |
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 |
import os
import requests
from fastapi import FastAPI, Request, Depends
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import Optional, Any
# Check whether we are executing inside a Hugging Face Space
SPACE_NAME = os.getenv("SPACE_NAME", default=None)
if SPACE_NAME is not None:
print(f"Running inside {SPACE_NAME} Space.")
try:
# Try to auto-login using the Space's environment variables
login(automatically=True)
except Exception as e:
print(f"Failed to auto-login ({str(e)}). Manually check the HF_ACCESS_TOKEN environment variable.")
sys.exit(1)
try:
HUGGINGFACE_TOKEN = os.environ['HF_ACCESS_TOKEN']
except KeyError:
print('The environment variable "HF_ACCESS_TOKEN" is not found. Please configure it correctly in your Space.')
sys.exit(1)
# Set up the API endpoint and headers
model_id = "152334H/miqu-1-70b-sf"
endpoint = f"https://api-inference.huggingface.co/models/{model_id}"
headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
# App definition
app = FastAPI()
# Helper function to read raw request bodies
async def parse_raw(request: Request):
return await request.body()
# Generate text using the Inference API
def generate_text(prompt: str) -> str:
data = {
"inputs": prompt,
"options": {
"max_new_tokens": 200,
"temperature": 0.7,
"top_p": 0.95,
"use_cache": False,
},
}
response = requests.post(endpoint, headers=headers, json=data)
return response.json()["generated_text"]
# Route for generating text
@app.post("/generate_text")
async def generate_text_route(data: BaseModel = Depends(parse_raw)):
input_text = data.raw.decode("utf-8")
if not input_text or len(input_text) <= 0:
return JSONResponse({"error": "Empty input received."}, status_code=400)
return {"output": generate_text(input_text)}
# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")
|