Update main.py
Browse files
main.py
CHANGED
|
@@ -3,23 +3,10 @@ from fastapi.responses import JSONResponse, FileResponse
|
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
import json
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
if HF_TOKEN is None:
|
| 12 |
-
raise ValueError("Please set the HF_TOKEN environment variable.")
|
| 13 |
-
|
| 14 |
-
# Model and API setup
|
| 15 |
-
model_id = 'meta-llama/Meta-Llama-3-8B-Instruct'
|
| 16 |
-
API_URL = "https://api-inference.huggingface.co/models/" + model_id
|
| 17 |
-
|
| 18 |
-
Client = InferenceClient(
|
| 19 |
-
API_URL,
|
| 20 |
-
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
|
| 24 |
SYSTEM_MESSAGE = (
|
| 25 |
"You are a helpful, respectful and honest assistant. Always answer as helpfully "
|
|
@@ -57,9 +44,15 @@ def respond(message, history: list[tuple[str, str]]):
|
|
| 57 |
for message in response: # Handle regular iteration
|
| 58 |
yield message.choices[0].delta.content
|
| 59 |
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
@app.post("/generate/")
|
| 65 |
async def generate(request: Request):
|
|
|
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
import json
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
SYSTEM_MESSAGE = (
|
| 12 |
"You are a helpful, respectful and honest assistant. Always answer as helpfully "
|
|
|
|
| 44 |
for message in response: # Handle regular iteration
|
| 45 |
yield message.choices[0].delta.content
|
| 46 |
|
| 47 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 48 |
|
| 49 |
+
app.add_middleware(
|
| 50 |
+
CORSMiddleware,
|
| 51 |
+
allow_origins=["https://artixiban-ll3.static.hf.space"], # Allow only this origin
|
| 52 |
+
allow_credentials=True,
|
| 53 |
+
allow_methods=["*"], # Allow all methods (GET, POST, etc.)
|
| 54 |
+
allow_headers=["*"], # Allow all headers
|
| 55 |
+
)
|
| 56 |
|
| 57 |
@app.post("/generate/")
|
| 58 |
async def generate(request: Request):
|