tharunk07 commited on
Commit
b5ff879
·
1 Parent(s): 80d0760

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +14 -0
  2. app.py +65 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ FROM python:3.9
3
+
4
+ RUN useradd -m -u 1000 user
5
+ USER user
6
+ ENV PATH="/home/user/.local/bin:$PATH"
7
+
8
+ WORKDIR /app
9
+
10
+ COPY --chown=user ./requirements.txt requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
12
+
13
+ COPY --chown=user . /app
14
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, set_seed
2
+ from fastapi import FastAPI,Form
3
+ from fastapi.responses import JSONResponse
4
+
5
+ app = FastAPI()
6
+
7
+ class GPTGenerator:
8
+ """
9
+ A wrapper class for GPT-2 text generation using Hugging Face's transformers library.
10
+
11
+ Attributes:
12
+ generator (pipeline): A pre-trained GPT-2 model for text generation.
13
+ """
14
+ def __init__(self):
15
+ self.generator = pipeline('text-generation', model='gpt2')
16
+ set_seed(42)
17
+ def text_generation(self,user_input) ->str:
18
+ """
19
+ Generates text based on the given user input using the GPT-2 model.
20
+
21
+ Args:
22
+ user_input (str): The text prompt provided by the user.
23
+
24
+ Returns:
25
+ str: The generated text output from GPT-2.
26
+ """
27
+ self.output = self.generator(user_input, max_length=60, num_return_sequences=5)
28
+ return self.output[0]['generated_text']
29
+
30
+ generator = GPTGenerator()
31
+
32
+ @app.post("/text-generator")
33
+ async def generate(user_prompt : str = Form(...)) -> JSONResponse:
34
+ """
35
+ Handles text generation requests via a POST request.
36
+
37
+ This endpoint takes a user-provided text prompt, generates text using the GPT-2 model,
38
+ and returns the output in JSON format.
39
+
40
+ Args:
41
+ user_prompt (str): The text prompt submitted by the user via a form.
42
+
43
+ Returns:
44
+ JSONResponse: A JSON response containing the generated text.
45
+
46
+ Raises:
47
+ Exception: If there is an error in text generation, it is raised to the caller.
48
+ """
49
+ if user_prompt is not None:
50
+ try:
51
+ generated_output = generator.text_generation(user_prompt)
52
+ except Exception as e:
53
+ raise e
54
+ return JSONResponse(content = {"generated_text":generated_output})
55
+
56
+ @app.get("/")
57
+ def health_check() -> dict:
58
+ """
59
+ Performs a health check to verify if the API is running.
60
+
61
+ Returns:
62
+ dict: A simple JSON response indicating the API status.
63
+ """
64
+ return {"message":"API is running"}
65
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch