X

Transformers
code
Not-For-All-Audiences
legal
anything
medical
biology
CLASSIFIED-HEX commited on
Commit
c2ad250
·
verified ·
1 Parent(s): 8faba80

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +65 -0
handler.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # handler.py
2
+
3
+ import os
4
+ import requests
5
+ from fastapi import APIRouter, HTTPException
6
+ from pydantic import BaseModel
7
+ from typing import Optional
8
+ from dotenv import load_dotenv
9
+
10
+ # Load secrets from .env
11
+ load_dotenv()
12
+
13
+ # Securely read environment variables
14
+ HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
15
+ HUGGINGFACE_MODEL_URL = os.getenv("HUGGINGFACE_MODEL_URL")
16
+
17
+ # FastAPI router setup
18
+ router = APIRouter()
19
+
20
+ # Input format
21
+ class PromptInput(BaseModel):
22
+ prompt: str
23
+ max_tokens: Optional[int] = 250
24
+ temperature: Optional[float] = 0.7
25
+ top_p: Optional[float] = 0.95
26
+ top_k: Optional[int] = 50
27
+ repetition_penalty: Optional[float] = 1.2
28
+
29
+ # Main endpoint
30
+ @router.post("/generate")
31
+ async def generate_text(input_data: PromptInput):
32
+ if not HUGGINGFACE_API_TOKEN or not HUGGINGFACE_MODEL_URL:
33
+ raise HTTPException(status_code=500, detail="Hugging Face API token or model URL not configured.")
34
+
35
+ headers = {
36
+ "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"
37
+ }
38
+
39
+ payload = {
40
+ "inputs": input_data.prompt,
41
+ "parameters": {
42
+ "max_new_tokens": input_data.max_tokens,
43
+ "temperature": input_data.temperature,
44
+ "top_p": input_data.top_p,
45
+ "top_k": input_data.top_k,
46
+ "repetition_penalty": input_data.repetition_penalty
47
+ }
48
+ }
49
+
50
+ try:
51
+ response = requests.post(HUGGINGFACE_MODEL_URL, headers=headers, json=payload)
52
+
53
+ if response.status_code != 200:
54
+ raise HTTPException(status_code=response.status_code, detail=response.json())
55
+
56
+ result = response.json()
57
+ generated_text = result[0].get("generated_text") if isinstance(result, list) else result.get("generated_text", "")
58
+
59
+ return {
60
+ "status": "success",
61
+ "output": generated_text
62
+ }
63
+
64
+ except Exception as e:
65
+ raise HTTPException(status_code=500, detail=f"Text generation failed: {str(e)}")