File size: 2,663 Bytes
6ee90aa
 
0551907
6ee90aa
27a4aad
e5591d2
6f96f84
d409c7c
b837945
9f95009
 
d409c7c
9f95009
 
d409c7c
9f95009
 
d409c7c
 
 
9f95009
b837945
 
 
 
 
 
 
 
 
 
b7c9474
 
 
 
 
 
080b041
6ee90aa
 
 
 
03ee1c6
79e06e3
 
 
1b710b0
6ee90aa
710a16c
8db8221
6468fe6
 
 
6ee90aa
6468fe6
8db8221
 
6468fe6
 
 
8db8221
 
 
710a16c
03ee1c6
 
 
8055e21
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
65
66
67
68
69
70
71
72
73
74
75
76
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
from fastapi.responses import HTMLResponse

app = FastAPI()

# Montage du répertoire statique
app.mount("/static", StaticFiles(directory="static"), name="static")

# Route racine pour servir index.html
@app.get("/", include_in_schema=False)
async def root():
    with open("static/index.html", "r") as html_file:
        content = html_file.read()
    return HTMLResponse(content=content)

# 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}"}

# 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(input_text: str) -> str:
    data = {"inputs": input_text}
    try:
        response = requests.post(endpoint, json=data, headers=headers)
        response.raise_for_status()  # Raise an exception for non-2xx status codes

        print(response.json())  # Print the entire response object

        return response.json()["generated_text"]
    except requests.exceptions.RequestException as e:
        print(f"RequestException: {e}")
        return ""
    except KeyError as e:
        print(f"KeyError: {e}. Check the response object for the correct key.")
        return ""

# Route for generating text
@app.post("/generate_text")
async def generate_text_route(data: BaseModel = Depends(parse_raw)):
    input_text = data.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")