Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ from fastapi import FastAPI
|
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import JSONResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
5 |
import numpy as np
|
6 |
import argparse
|
7 |
import os
|
@@ -25,6 +27,7 @@ app.add_middleware(
|
|
25 |
allow_headers=["*"],
|
26 |
)
|
27 |
|
|
|
28 |
|
29 |
@app.get("/invert")
|
30 |
async def invert(text: str):
|
@@ -33,6 +36,26 @@ async def invert(text: str):
|
|
33 |
"inverted": text[::-1],
|
34 |
}
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
@app.get("/data")
|
38 |
async def get_data():
|
|
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import JSONResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
+
from fastapi import Request
|
6 |
+
import requests
|
7 |
import numpy as np
|
8 |
import argparse
|
9 |
import os
|
|
|
27 |
allow_headers=["*"],
|
28 |
)
|
29 |
|
30 |
+
HF_TOKEN = os.getenv("REACT_APP_HF_TOKEN")
|
31 |
|
32 |
@app.get("/invert")
|
33 |
async def invert(text: str):
|
|
|
36 |
"inverted": text[::-1],
|
37 |
}
|
38 |
|
39 |
+
@app.post("/api/chat")
|
40 |
+
async def chat(request: Request):
|
41 |
+
data = await request.json()
|
42 |
+
user_message = data.get("message", "")
|
43 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
44 |
+
payload = {
|
45 |
+
"inputs": {
|
46 |
+
"past_user_inputs": [],
|
47 |
+
"generated_responses": [],
|
48 |
+
"text": user_message
|
49 |
+
}
|
50 |
+
}
|
51 |
+
response = requests.post(
|
52 |
+
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3",
|
53 |
+
headers=headers,
|
54 |
+
json=payload
|
55 |
+
)
|
56 |
+
result = response.json()
|
57 |
+
return {"response": result}
|
58 |
+
|
59 |
|
60 |
@app.get("/data")
|
61 |
async def get_data():
|