rohanshaw commited on
Commit
f4f976d
1 Parent(s): 24873c5

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +92 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:latest
2
+
3
+ WORKDIR /
4
+
5
+ COPY ./requirements.txt .
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
8
+
9
+ COPY . .
10
+
11
+ EXPOSE 7860
12
+
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+
7
+ import logging
8
+
9
+ app = FastAPI()
10
+
11
+ # Add logging
12
+
13
+ logging.basicConfig(level=logging.INFO)
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+ formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
18
+
19
+ handler = logging.StreamHandler()
20
+
21
+ handler.setFormatter(formatter)
22
+
23
+ logger.addHandler(handler)
24
+
25
+ # Add CORS
26
+
27
+ origins = ["*"]
28
+
29
+ app.add_middleware(
30
+ CORSMiddleware,
31
+ allow_origins=origins,
32
+ allow_credentials=True,
33
+ allow_methods=["GET", "POST", "PUT", "DELETE"],
34
+ allow_headers=["*"],
35
+ )
36
+
37
+ intent_model = AutoModelForCausalLM.from_pretrained("llmware/slim-intent")
38
+ intent_tokenizer = AutoTokenizer.from_pretrained("llmware/slim-intent")
39
+
40
+ sentiment_model = AutoModelForCausalLM.from_pretrained("llmware/slim-sentiment")
41
+ sentiment_tokenizer = AutoTokenizer.from_pretrained("llmware/slim-sentiment")
42
+
43
+ def getResponse(model, tokenizer, text, params):
44
+ function = "classify"
45
+
46
+ prompt = "<human>: " + text + "\n" + f"<{function}> {params} </{function}>\n<bot>:"
47
+
48
+ inputs = tokenizer(prompt, return_tensors="pt")
49
+ start_of_input = len(inputs.input_ids[0])
50
+
51
+ outputs = model.generate(
52
+ inputs.input_ids.to('cpu'),
53
+ eos_token_id=tokenizer.eos_token_id,
54
+ pad_token_id=tokenizer.eos_token_id,
55
+ do_sample=True,
56
+ temperature=0.3,
57
+ max_new_tokens=100
58
+ )
59
+
60
+ output = tokenizer.decode(outputs[0][start_of_input:], skip_special_tokens=True)
61
+
62
+ return output
63
+
64
+ @app.get("/")
65
+ def read_root():
66
+ return {
67
+ "message": "API running successfully",
68
+ "endpoints": [
69
+ "/api/sentiment/",
70
+ "/api/intent/"
71
+ ]
72
+ }
73
+
74
+ @app.post("/api/intent/")
75
+ def intentResponse(text: str):
76
+ params = "intent"
77
+ try:
78
+ responses = getResponse(intent_model, intent_tokenizer, text, params)
79
+ return responses
80
+ except Exception as e:
81
+ logger.exception(e)
82
+ return {"API Error": str(e)}
83
+
84
+ @app.post("/api/sentiment/")
85
+ def sentimentResponse(text: str):
86
+ params = "sentiment"
87
+ try:
88
+ responses = getResponse(sentiment_model, sentiment_tokenizer, text, params)
89
+ return responses
90
+ except Exception as e:
91
+ logger.exception(e)
92
+ return {"API Error": str(e)}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ fastapi
4
+ uvicorn