Spaces:
Paused
Paused
Upload 9 files
Browse filesuploading code v1
- Dockerfile +27 -0
- README.md +4 -3
- app.py +20 -0
- index/config +0 -0
- index/documents +0 -0
- index/embeddings +0 -0
- main.py +50 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 current directory contents into the container at /code
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 12 |
+
|
| 13 |
+
# Set up a new user named "user" with user ID 1000
|
| 14 |
+
RUN useradd -m -u 1000 user
|
| 15 |
+
# Switch to the "user" user
|
| 16 |
+
USER user
|
| 17 |
+
# Set home to the user's home directory
|
| 18 |
+
ENV HOME=/home/user \
|
| 19 |
+
PATH=/home/user/.local/bin:$PATH
|
| 20 |
+
|
| 21 |
+
# Set the working directory to the user's home directory
|
| 22 |
+
WORKDIR $HOME/app
|
| 23 |
+
|
| 24 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 25 |
+
COPY --chown=user . $HOME/app
|
| 26 |
+
|
| 27 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Text Generation
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: green
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
---
|
| 10 |
|
| 11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
| 6 |
+
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
| 7 |
+
app = FastAPI(docs_url="/")
|
| 8 |
+
|
| 9 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@app.get("/generate")
|
| 13 |
+
def generate(text: str):
|
| 14 |
+
"""
|
| 15 |
+
Using the text2text-generation pipeline from `transformers`, generate text
|
| 16 |
+
from the given input text. The model used is `google/flan-t5-small`, which
|
| 17 |
+
can be found [here](https://huggingface.co/google/flan-t5-small).
|
| 18 |
+
"""
|
| 19 |
+
output = pipe(text)
|
| 20 |
+
return {"output": output[0]["generated_text"]}
|
index/config
ADDED
|
Binary file (288 Bytes). View file
|
|
|
index/documents
ADDED
|
Binary file (41 kB). View file
|
|
|
index/embeddings
ADDED
|
Binary file (29.4 kB). View file
|
|
|
main.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
# from transformers import pipeline
|
| 3 |
+
from txtai.embeddings import Embeddings
|
| 4 |
+
from txtai.pipeline import Extractor
|
| 5 |
+
|
| 6 |
+
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
| 7 |
+
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
| 8 |
+
app = FastAPI(docs_url="/")
|
| 9 |
+
|
| 10 |
+
# Create embeddings model with content support
|
| 11 |
+
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": True})
|
| 12 |
+
embeddings.load('index')
|
| 13 |
+
|
| 14 |
+
# Create extractor instance
|
| 15 |
+
extractor = Extractor(embeddings, "google/flan-t5-base")
|
| 16 |
+
|
| 17 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@app.get("/generate")
|
| 21 |
+
def generate(text: str):
|
| 22 |
+
"""
|
| 23 |
+
Using the text2text-generation pipeline from `transformers`, generate text
|
| 24 |
+
from the given input text. The model used is `google/flan-t5-small`, which
|
| 25 |
+
can be found [here](https://huggingface.co/google/flan-t5-small).
|
| 26 |
+
"""
|
| 27 |
+
output = pipe(text)
|
| 28 |
+
return {"output": output[0]["generated_text"]}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def prompt(question):
|
| 32 |
+
return f"""Answer the following question using only the context below. Say 'no answer' when the question can't be answered.
|
| 33 |
+
Question: {question}
|
| 34 |
+
Context: """
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def search(query, question=None):
|
| 38 |
+
# Default question to query if empty
|
| 39 |
+
if not question:
|
| 40 |
+
question = query
|
| 41 |
+
|
| 42 |
+
return extractor([("answer", query, prompt(question), False)])[0][1]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@app.get("/rag")
|
| 46 |
+
def rag(question: str):
|
| 47 |
+
# question = "what is the document about?"
|
| 48 |
+
answer = search(question)
|
| 49 |
+
# print(question, answer)
|
| 50 |
+
return {answer}
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.74.*
|
| 2 |
+
requests==2.27.*
|
| 3 |
+
uvicorn[standard]==0.17.*
|
| 4 |
+
sentencepiece==0.1.*
|
| 5 |
+
txtai==6.0.*
|