Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,22 +1,26 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
|
|
|
| 6 |
app.add_middleware(
|
| 7 |
CORSMiddleware,
|
| 8 |
allow_origins=["*"],
|
| 9 |
-
allow_credentials=True,
|
| 10 |
allow_methods=["*"],
|
| 11 |
-
allow_headers=["*"]
|
| 12 |
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return {"message": "Backend is working"}
|
| 17 |
|
| 18 |
@app.get("/chat")
|
| 19 |
def chat(prompt: str):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Load a chat model (you can choose a bigger one)
|
| 7 |
+
generator = pipeline('text-generation', model='gpt2') # gpt2 is small; for better, use GPT-Neo
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# Allow CORS for your frontend
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"],
|
|
|
|
| 15 |
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"]
|
| 17 |
)
|
| 18 |
|
| 19 |
+
class Prompt(BaseModel):
|
| 20 |
+
prompt: str
|
|
|
|
| 21 |
|
| 22 |
@app.get("/chat")
|
| 23 |
def chat(prompt: str):
|
| 24 |
+
# Generate response
|
| 25 |
+
response = generator(prompt, max_length=100, do_sample=True)[0]['generated_text']
|
| 26 |
+
return {"reply": response}
|