gruhit-patel commited on
Commit
239290b
0 Parent(s):

Quote Generator Backend init

Browse files
Files changed (6) hide show
  1. .gitattributes +35 -0
  2. Dockerfile +15 -0
  3. QuoteGenerator.py +35 -0
  4. README.md +10 -0
  5. main.py +35 -0
  6. requirements.txt +91 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Get the image of python
2
+ FROM python:3.9
3
+
4
+ # Copy all the files
5
+ COPY . .
6
+
7
+ # Set the work directory
8
+ WORKDIR /
9
+
10
+ # Install the requirements
11
+ RUN pip install --no-cache-dir --upgrade -r /requirements.txt
12
+
13
+ COPY . .
14
+
15
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
QuoteGenerator.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, TFGPT2LMHeadModel, pipeline
2
+ from transformers.pipelines import TextGenerationPipeline
3
+ from typing import Union
4
+
5
+ class QuoteGenerator():
6
+ def __init__(self, model_name:str='gruhit13/quote_generator_v1'):
7
+ self.model_name = model_name
8
+ self.quote_generator: TextGenerationPipeline
9
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
10
+ self.model = TFGPT2LMHeadModel.from_pretrained(self.model_name)
11
+ self.default_tags = 'love,life'
12
+ print("Model has been loaded")
13
+
14
+ def load_generator(self) -> None:
15
+ self.quote_generator = pipeline('text-generation', model=self.model, tokenizer=self.tokenizer)
16
+ print("Pipeline has been generated")
17
+
18
+ def preprocess_tags(self, tags: Union[None, str] = None) -> str:
19
+ if tags is None:
20
+ tags = self.default_tags
21
+
22
+ return self.tokenizer.bos_token + tags + '<bot>:'
23
+
24
+ def generate_quote(self, tags:Union[None, str]=None,
25
+ min_length: int=3, max_length:int=60,
26
+ top_p:float=0.9, top_k:int=5):
27
+
28
+ tags = self.preprocess_tags(tags)
29
+ print("Tags = ", tags)
30
+ output = self.quote_generator(tags, min_length=min_length, max_length=max_length,
31
+ temperature=1.0, top_k=5, top_p=top_p, early_stopping=True,
32
+ num_beams=4)
33
+
34
+ return output[0]['generated_text']
35
+
README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Quote Generator Backend
3
+ emoji: 👁
4
+ colorFrom: pink
5
+ colorTo: green
6
+ sdk: docker
7
+ pinned: false
8
+ ---
9
+
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
main.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from QuoteGenerator import QuoteGenerator
4
+ from typing import Union
5
+ from pydantic import BaseModel
6
+
7
+ class QuoteRequest(BaseModel):
8
+ tags: Union[None, str]
9
+
10
+ app = FastAPI()
11
+
12
+ origins = ['https://quote-generator.streamlit.app/']
13
+
14
+ # Setting up cors
15
+ app.add_middleware(
16
+ CORSMiddleware,
17
+ allow_origins=origins,
18
+ allow_credentials=True,
19
+ allow_methods=["GET", "POST"],
20
+ allow_headers=["*"]
21
+ )
22
+
23
+ quote_generator = QuoteGenerator()
24
+ quote_generator.load_generator()
25
+
26
+ @app.get("/")
27
+ def root(request: Request):
28
+ print("URL ", request.url.path)
29
+ return {"message": "This is the website for quote-generator"}
30
+
31
+ @app.post("/generate_quote")
32
+ def generate_quote(req: QuoteRequest):
33
+ print("Tags: ", req.tags)
34
+ generated_quote_oup = quote_generator.generate_quote(req.tags)
35
+ return {'quote': generated_quote_oup}
requirements.txt ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.1.0
2
+ altair==5.3.0
3
+ annotated-types==0.6.0
4
+ anyio==4.3.0
5
+ astunparse==1.6.3
6
+ attrs==23.2.0
7
+ blinker==1.7.0
8
+ cachetools==5.3.3
9
+ certifi==2024.2.2
10
+ charset-normalizer==3.3.2
11
+ click==8.1.7
12
+ colorama==0.4.6
13
+ fastapi==0.110.1
14
+ filelock==3.13.3
15
+ flatbuffers==24.3.25
16
+ fsspec==2024.3.1
17
+ gast==0.5.4
18
+ gitdb==4.0.11
19
+ GitPython==3.1.43
20
+ google-auth==2.29.0
21
+ google-auth-oauthlib==1.2.0
22
+ google-pasta==0.2.0
23
+ grpcio==1.62.1
24
+ h11==0.14.0
25
+ h5py==3.10.0
26
+ httptools==0.6.1
27
+ huggingface-hub==0.22.2
28
+ idna==3.6
29
+ Jinja2==3.1.3
30
+ jsonschema==4.21.1
31
+ jsonschema-specifications==2023.12.1
32
+ keras==2.15.0
33
+ libclang==18.1.1
34
+ Markdown==3.6
35
+ markdown-it-py==3.0.0
36
+ MarkupSafe==2.1.5
37
+ mdurl==0.1.2
38
+ ml-dtypes==0.2.0
39
+ numpy==1.26.4
40
+ oauthlib==3.2.2
41
+ opt-einsum==3.3.0
42
+ packaging==24.0
43
+ pandas==2.2.1
44
+ pillow==10.3.0
45
+ protobuf==4.25.3
46
+ pyarrow==15.0.2
47
+ pyasn1==0.6.0
48
+ pyasn1_modules==0.4.0
49
+ pydantic==2.6.4
50
+ pydantic_core==2.16.3
51
+ pydeck==0.8.1b0
52
+ Pygments==2.17.2
53
+ python-dateutil==2.9.0.post0
54
+ python-dotenv==1.0.1
55
+ pytz==2024.1
56
+ PyYAML==6.0.1
57
+ referencing==0.34.0
58
+ regex==2023.12.25
59
+ requests==2.31.0
60
+ requests-oauthlib==2.0.0
61
+ rich==13.7.1
62
+ rpds-py==0.18.0
63
+ rsa==4.9
64
+ safetensors==0.4.2
65
+ six==1.16.0
66
+ smmap==5.0.1
67
+ sniffio==1.3.1
68
+ starlette==0.37.2
69
+ tenacity==8.2.3
70
+ tensorboard==2.15.2
71
+ tensorboard-data-server==0.7.2
72
+ tensorflow==2.15.0
73
+ tensorflow-estimator==2.15.0
74
+ tensorflow-intel==2.15.0
75
+ tensorflow-io-gcs-filesystem==0.31.0
76
+ termcolor==2.4.0
77
+ tokenizers==0.15.2
78
+ toml==0.10.2
79
+ toolz==0.12.1
80
+ tornado==6.4
81
+ tqdm==4.66.2
82
+ transformers==4.39.3
83
+ typing_extensions==4.11.0
84
+ tzdata==2024.1
85
+ urllib3==2.2.1
86
+ uvicorn==0.29.0
87
+ watchdog==4.0.0
88
+ watchfiles==0.21.0
89
+ websockets==12.0
90
+ Werkzeug==3.0.2
91
+ wrapt==1.14.1