ayazfau commited on
Commit
d14a20e
1 Parent(s): b47ea42

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +30 -0
  2. app.py +23 -0
  3. requirements.txt +7 -0
  4. templates/index.html +39 -0
Dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.9 image
2
+ FROM python:3.9
3
+
4
+ # Set the working directory to /code
5
+ WORKDIR /code
6
+
7
+ # Copy the requirements.txt file to the container at /code
8
+ COPY requirements.txt /code/
9
+
10
+ # Install the requirements specified in requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
+
13
+ # Set up a new user named 'user'
14
+ RUN useradd -ms /bin/bash user
15
+
16
+ # Set environment variables for the user
17
+ ENV HOME=/home/user \
18
+ PATH=/home/user/.local/bin:$PATH
19
+
20
+ # Set the working directory to the user's home directory
21
+ WORKDIR $HOME/app
22
+
23
+ # Copy the current directory contents into the container at $HOME/app, setting the owner to 'user'
24
+ COPY --chown=user . $HOME/app
25
+
26
+ # Switch to the "user" user
27
+ USER user
28
+
29
+ # Start the FastAPI app on port 7860
30
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.templating import Jinja2Templates
4
+ from transformers import pipeline
5
+
6
+ # FastAPI app instance
7
+ app = FastAPI()
8
+
9
+ # Initializing the text generation pipeline
10
+ pipe = pipeline("text2text-generation", model="google/flan-t5-large")
11
+
12
+ # Jinja2 template configuration
13
+ templates = Jinja2Templates(directory="templates")
14
+
15
+ @app.get("/", response_class=HTMLResponse)
16
+ async def home(request: Request):
17
+ return templates.TemplateResponse("index.html", {"request": request})
18
+
19
+ @app.get("/generate")
20
+ def generate(text: str):
21
+ # Using the pipeline to generate text from given input
22
+ output = pipe(text, max_length=50) # Adjust max_length as needed
23
+ return {"output": output[0]['generated_text']}
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ uvicorn[standard]==0.17.*
4
+ sentencepiece==0.1.*
5
+ torch==1.11.*
6
+ transformers==4.*
7
+ gradio
templates/index.html ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!-- templates/index.html -->
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Chat UI</title>
8
+ </head>
9
+ <body>
10
+ <h1>Chat UI</h1>
11
+ <form action="/generate" method="get">
12
+ <label for="user_input">User Input:</label><br>
13
+ <input type="text" id="user_input" name="text"><br><br>
14
+ <button type="submit">Generate Response</button>
15
+ </form>
16
+ <div id="output">
17
+ <h2>Output:</h2>
18
+ <p id="response"></p>
19
+ </div>
20
+
21
+ <script>
22
+ async function generateResponse() {
23
+ const userInput = document.getElementById("user_input").value;
24
+ const responseElement = document.getElementById("response");
25
+
26
+ const response = await fetch(`/generate?text=${encodeURIComponent(userInput)}`);
27
+ const responseData = await response.json();
28
+
29
+ responseElement.textContent = responseData.output;
30
+ }
31
+
32
+ const form = document.querySelector("form");
33
+ form.addEventListener("submit", function(event) {
34
+ event.preventDefault();
35
+ generateResponse();
36
+ });
37
+ </script>
38
+ </body>
39
+ </html>