Spaces:
Sleeping
Sleeping
update
Browse files- .gitignore +1 -0
- Dockerfile +12 -0
- main.py +65 -0
- requirements.txt +7 -0
.gitignore
CHANGED
@@ -1 +1,2 @@
|
|
1 |
.env
|
|
|
|
1 |
.env
|
2 |
+
__pycache__
|
Dockerfile
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10.12
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY . /app
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
8 |
+
|
9 |
+
EXPOSE 8000
|
10 |
+
|
11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
12 |
+
|
main.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
2 |
+
import torch
|
3 |
+
from datasets import load_dataset
|
4 |
+
import pandas as pd
|
5 |
+
import re
|
6 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
7 |
+
from fastapi import FastAPI
|
8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
9 |
+
from pydantic import BaseModel
|
10 |
+
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# Load your model and tokenizer
|
15 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
16 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
+
model.to(device)
|
19 |
+
|
20 |
+
# Add CORS middleware to allow any origin
|
21 |
+
app.add_middleware(
|
22 |
+
CORSMiddleware,
|
23 |
+
allow_origins=["*"], # Allows all origins
|
24 |
+
allow_credentials=True,
|
25 |
+
allow_methods=["*"], # Allows all methods (GET, POST, etc.)
|
26 |
+
allow_headers=["*"], # Allows all headers
|
27 |
+
)
|
28 |
+
|
29 |
+
@app.get("/")
|
30 |
+
def root():
|
31 |
+
return {"Hello": "World"}
|
32 |
+
|
33 |
+
# Define the Pydantic model to parse JSON input
|
34 |
+
class HistoryRequest(BaseModel):
|
35 |
+
user: list[str]
|
36 |
+
ai: list[str]
|
37 |
+
|
38 |
+
@app.post("/generate")
|
39 |
+
def generate_response(history: HistoryRequest):
|
40 |
+
combined_prompt = ""
|
41 |
+
|
42 |
+
# Iterate over user and AI messages
|
43 |
+
for user_message, ai_message in zip(history.user, history.ai):
|
44 |
+
combined_prompt += f"<user> {user_message}\n<AI> {ai_message}\n"
|
45 |
+
|
46 |
+
# Include the last user message in the prompt for response generation
|
47 |
+
if history.user:
|
48 |
+
combined_prompt += f"<user> {history.user[-1]}\n<AI>"
|
49 |
+
|
50 |
+
# Tokenize and generate response
|
51 |
+
inputs = tokenizer.encode(combined_prompt, return_tensors="pt").to(device)
|
52 |
+
outputs = model.generate(
|
53 |
+
inputs,
|
54 |
+
max_length=150, # Adjust length as needed
|
55 |
+
num_beams=5,
|
56 |
+
early_stopping=True,
|
57 |
+
no_repeat_ngram_size=2,
|
58 |
+
temperature=0.7,
|
59 |
+
top_k=50,
|
60 |
+
top_p=0.95
|
61 |
+
)
|
62 |
+
|
63 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
64 |
+
response = response.replace(combined_prompt, "").split(".")[0]
|
65 |
+
return response
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
transformers==4.42.4
|
3 |
+
torch==2.3.1
|
4 |
+
datasets==2.21.0
|
5 |
+
pandas==2.1.4
|
6 |
+
uvicorn[standard]
|
7 |
+
fastapi[all]
|