baadror commited on
Commit
2556944
·
1 Parent(s): 3f11d02

adding gradio

Browse files
Files changed (6) hide show
  1. Dockerfile +13 -8
  2. main.py +16 -15
  3. requirements.txt +0 -3
  4. static/index.html +0 -36
  5. static/script.js +0 -21
  6. static/style.css +0 -45
Dockerfile CHANGED
@@ -4,19 +4,24 @@
4
 
5
  FROM python:3.9
6
 
 
 
7
  # The two following lines are requirements for the Dev Mode to be functional
8
  # Learn more about the Dev Mode at https://huggingface.co/dev-mode-explorers
9
- RUN useradd -m -u 1000 user
10
- WORKDIR /app
11
-
12
- COPY --chown=user ./requirements.txt requirements.txt
13
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
-
15
- COPY --chown=user . /app
16
 
 
17
  USER user
18
 
19
  ENV HOME=/home/user \
20
  PATH=/home/user/.local/bin:$PATH
21
 
22
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
 
 
 
4
 
5
  FROM python:3.9
6
 
7
+ WORKDIR /code
8
+
9
  # The two following lines are requirements for the Dev Mode to be functional
10
  # Learn more about the Dev Mode at https://huggingface.co/dev-mode-explorers
11
+ COPY ./requirements.txt /code/requirements.txt
12
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
 
 
 
 
 
13
 
14
+ RUN useradd -m -u 1000 user
15
  USER user
16
 
17
  ENV HOME=/home/user \
18
  PATH=/home/user/.local/bin:$PATH
19
 
20
+ WORKDIR $HOME/app
21
+
22
+ COPY --chown=user . $HOME/app
23
+
24
+
25
+
26
+
27
+ CMD ["python",'main.py']
main.py CHANGED
@@ -1,25 +1,26 @@
1
- from fastapi import FastAPI
2
- from starlette.responses import FileResponse
3
- from starlette.staticfiles import StaticFiles
4
-
5
  from transformers import pipeline
6
 
 
 
7
  pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
8
 
9
- app = FastAPI()
10
 
11
- @app.get("/infer_t5")
12
- def t5(input):
13
  output = pipe_flan(input)
14
- return {"output": output[0]["generated_text"]}
 
 
 
 
 
 
 
 
 
15
 
 
16
 
17
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
18
 
19
- @app.get("/")
20
- def index() -> FileResponse:
21
- return FileResponse(path="/app/static/index.html", media_type="text/html")
22
 
23
- # @app.get('/')
24
- # def greet():
25
- # return {'Hello': 'Moti'}
 
 
 
 
 
1
  from transformers import pipeline
2
 
3
+ import gradio as gr
4
+
5
  pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
6
 
 
7
 
8
+ # @app.get("/infer_t5")
9
+ def pred(input):
10
  output = pipe_flan(input)
11
+ return output[0]["generated_text"]
12
+
13
+
14
+ demo = gr.Blocks()
15
+
16
+ with demo:
17
+ with gr.Row():
18
+ input_text = gr.Textbox(label='Input Text',lines=5)
19
+ b1 = gr.Button('Submit')
20
+ output_text = gr.Textbox()
21
 
22
+ b1.click(fn = pred, inputs=input_text, outputs= output_text)
23
 
24
+ demo.launch()
25
 
 
 
 
26
 
 
 
 
requirements.txt CHANGED
@@ -1,7 +1,4 @@
1
- fastapi==0.74.*
2
- requests==2.27.*
3
  sentencepiece==0.1.*
4
  torch==1.11.*
5
  transformers==4.*
6
- uvicorn[standard]==0.17.*
7
  numpy==1.23.1
 
 
 
1
  sentencepiece==0.1.*
2
  torch==1.11.*
3
  transformers==4.*
 
4
  numpy==1.23.1
static/index.html DELETED
@@ -1,36 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>Fast API 🤗 Space served with Uvicorn</title>
7
- <link rel="stylesheet" href="style.css" />
8
- <script type="module" src="script.js"></script>
9
- </head>
10
- <body>
11
- <main>
12
- <section id="text-gen">
13
- <h1>Text generation using Flan T5</h1>
14
- <p>
15
- Model:
16
- <a
17
- href="https://huggingface.co/google/flan-t5-small"
18
- rel="noreferrer"
19
- target="_blank"
20
- >google/flan-t5-small</a
21
- >
22
- </p>
23
- <form class="text-gen-form">
24
- <label for="text-gen-input">Text prompt</label>
25
- <input
26
- id="text-gen-input"
27
- type="text"
28
- value="English: Translate There are many ducks. German:"
29
- />
30
- <button id="text-gen-submit">Submit</button>
31
- <p class="text-gen-output"></p>
32
- </form>
33
- </section>
34
- </main>
35
- </body>
36
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
static/script.js DELETED
@@ -1,21 +0,0 @@
1
- const textGenForm = document.querySelector(".text-gen-form");
2
-
3
- const translateText = async (text) => {
4
- const inferResponse = await fetch(`infer_t5?input=${text}`);
5
- const inferJson = await inferResponse.json();
6
-
7
- return inferJson.output;
8
- };
9
-
10
- textGenForm.addEventListener("submit", async (event) => {
11
- event.preventDefault();
12
-
13
- const textGenInput = document.getElementById("text-gen-input");
14
- const textGenParagraph = document.querySelector(".text-gen-output");
15
-
16
- try {
17
- textGenParagraph.textContent = await translateText(textGenInput.value);
18
- } catch (err) {
19
- console.error(err);
20
- }
21
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
static/style.css DELETED
@@ -1,45 +0,0 @@
1
- body {
2
- --text: hsl(0 0% 15%);
3
- padding: 2.5rem;
4
- font-family: sans-serif;
5
- color: var(--text);
6
- }
7
-
8
- body.dark-theme {
9
- --text: hsl(0 0% 90%);
10
- background-color: hsl(223 39% 7%);
11
- }
12
-
13
- main {
14
- max-width: 80rem;
15
- text-align: center;
16
- }
17
-
18
- section {
19
- display: flex;
20
- flex-direction: column;
21
- align-items: center;
22
- }
23
-
24
- a {
25
- color: var(--text);
26
- }
27
-
28
- form {
29
- width: 30rem;
30
- margin: 0 auto;
31
- }
32
-
33
- input {
34
- width: 100%;
35
- }
36
-
37
- button {
38
- cursor: pointer;
39
- }
40
-
41
- .text-gen-output {
42
- min-height: 1.2rem;
43
- margin: 1rem;
44
- border: 0.5px solid grey;
45
- }