Spaces:
Sleeping
Sleeping
upload files
Browse files- Dockerfile +23 -0
- app.py +33 -0
- index.html +127 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
WORKDIR /code
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
COPY requirements.txt /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 11 |
+
|
| 12 |
+
RUN useradd user
|
| 13 |
+
USER user
|
| 14 |
+
|
| 15 |
+
ENV HOME=/home/user \
|
| 16 |
+
PATH=/home/user/.local/bin:$PATH
|
| 17 |
+
|
| 18 |
+
COPY --chown=user:user . $HOME/app
|
| 19 |
+
|
| 20 |
+
WORKDIR $HOME/app
|
| 21 |
+
|
| 22 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 23 |
+
|
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import cohere
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
pipe = pipeline("text2text-generation", model="Helsinki-NLP/opus-mt-en-es")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
co = cohere.Client(api_key="mrCaHsq1SlpANdsCyu6lII16xxsBq2IULQxZ9hfq")
|
| 14 |
+
|
| 15 |
+
@app.get("/", response_class=HTMLResponse)
|
| 16 |
+
def get_home():
|
| 17 |
+
with open("index.html") as f:
|
| 18 |
+
return HTMLResponse(content=f.read())
|
| 19 |
+
|
| 20 |
+
@app.get("/generate/")
|
| 21 |
+
def generate(text: str):
|
| 22 |
+
output = pipe(text)
|
| 23 |
+
return {"output": output[0]['generated_text']}
|
| 24 |
+
|
| 25 |
+
@app.get("/cohereai/")
|
| 26 |
+
def cohereai(text: str):
|
| 27 |
+
response = co.generate(
|
| 28 |
+
model='command-r-plus',
|
| 29 |
+
prompt=text,
|
| 30 |
+
temperature=0.3,
|
| 31 |
+
max_tokens=100,
|
| 32 |
+
)
|
| 33 |
+
return {"output": response.generations[0].text.strip()}
|
index.html
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Text Generation API</title>
|
| 5 |
+
<link
|
| 6 |
+
rel="stylesheet"
|
| 7 |
+
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
|
| 8 |
+
/>
|
| 9 |
+
<style>
|
| 10 |
+
body {
|
| 11 |
+
background-color: #f8f9fa;
|
| 12 |
+
padding: 20px;
|
| 13 |
+
}
|
| 14 |
+
.container {
|
| 15 |
+
max-width: 600px;
|
| 16 |
+
margin: auto;
|
| 17 |
+
background: #fff;
|
| 18 |
+
padding: 20px;
|
| 19 |
+
border-radius: 8px;
|
| 20 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
| 21 |
+
}
|
| 22 |
+
h1 {
|
| 23 |
+
text-align: center;
|
| 24 |
+
margin-bottom: 20px;
|
| 25 |
+
}
|
| 26 |
+
.form-group {
|
| 27 |
+
margin-bottom: 15px;
|
| 28 |
+
}
|
| 29 |
+
.btn {
|
| 30 |
+
width: 100%;
|
| 31 |
+
}
|
| 32 |
+
.output {
|
| 33 |
+
margin-top: 20px;
|
| 34 |
+
padding: 10px;
|
| 35 |
+
background: #e9ecef;
|
| 36 |
+
border-radius: 8px;
|
| 37 |
+
max-height: 300px;
|
| 38 |
+
/* Example: limit height with overflow */
|
| 39 |
+
overflow-y: auto;
|
| 40 |
+
/* Example: add scrollbars if content exceeds */
|
| 41 |
+
}
|
| 42 |
+
.spinner-border {
|
| 43 |
+
display: none;
|
| 44 |
+
}
|
| 45 |
+
</style>
|
| 46 |
+
</head>
|
| 47 |
+
|
| 48 |
+
<body>
|
| 49 |
+
<div class="container">
|
| 50 |
+
<h1>Text Generation API</h1>
|
| 51 |
+
|
| 52 |
+
<form id="generateForm">
|
| 53 |
+
<div class="form-group">
|
| 54 |
+
<label for="generateText">Enter text:</label>
|
| 55 |
+
<input
|
| 56 |
+
type="text"
|
| 57 |
+
class="form-control"
|
| 58 |
+
id="generateText"
|
| 59 |
+
name="text"
|
| 60 |
+
required
|
| 61 |
+
/>
|
| 62 |
+
</div>
|
| 63 |
+
<button type="submit" class="btn btn-primary">
|
| 64 |
+
Generate with Hugging Face
|
| 65 |
+
</button>
|
| 66 |
+
<div class="spinner-border text-primary" role="status" id="hfSpinner">
|
| 67 |
+
<span class="sr-only">Loading...</span>
|
| 68 |
+
</div>
|
| 69 |
+
</form>
|
| 70 |
+
<div id="output" class="output"></div>
|
| 71 |
+
|
| 72 |
+
<form id="cohereForm">
|
| 73 |
+
<div class="form-group">
|
| 74 |
+
<label for="cohereText">Enter text:</label>
|
| 75 |
+
<input
|
| 76 |
+
type="text"
|
| 77 |
+
class="form-control"
|
| 78 |
+
id="cohereText"
|
| 79 |
+
name="text"
|
| 80 |
+
required
|
| 81 |
+
/>
|
| 82 |
+
</div>
|
| 83 |
+
<button type="submit" class="btn btn-success">
|
| 84 |
+
Generate with Cohere AI
|
| 85 |
+
</button>
|
| 86 |
+
<div
|
| 87 |
+
class="spinner-border text-success"
|
| 88 |
+
role="status"
|
| 89 |
+
id="cohereSpinner"
|
| 90 |
+
>
|
| 91 |
+
<span class="sr-only">Loading...</span>
|
| 92 |
+
</div>
|
| 93 |
+
</form>
|
| 94 |
+
<div id="cohereOutput" class="output"></div>
|
| 95 |
+
</div>
|
| 96 |
+
|
| 97 |
+
<script>
|
| 98 |
+
document
|
| 99 |
+
.getElementById("generateForm")
|
| 100 |
+
.addEventListener("submit", async function (event) {
|
| 101 |
+
event.preventDefault();
|
| 102 |
+
document.getElementById("hfSpinner").style.display = "inline-block";
|
| 103 |
+
const text = document.getElementById("generateText").value;
|
| 104 |
+
const response = await fetch(
|
| 105 |
+
`/generate/?text=${encodeURIComponent(text)}`
|
| 106 |
+
);
|
| 107 |
+
const data = await response.json();
|
| 108 |
+
document.getElementById("hfSpinner").style.display = "none";
|
| 109 |
+
document.getElementById("output").innerHTML = data.output; // Use innerHTML instead of textContent
|
| 110 |
+
});
|
| 111 |
+
document
|
| 112 |
+
.getElementById("cohereForm")
|
| 113 |
+
.addEventListener("submit", async function (event) {
|
| 114 |
+
event.preventDefault();
|
| 115 |
+
document.getElementById("cohereSpinner").style.display =
|
| 116 |
+
"inline-block";
|
| 117 |
+
const text = document.getElementById("cohereText").value;
|
| 118 |
+
const response = await fetch(
|
| 119 |
+
`/cohereai/?text=${encodeURIComponent(text)}`
|
| 120 |
+
);
|
| 121 |
+
const data = await response.json();
|
| 122 |
+
document.getElementById("cohereSpinner").style.display = "none";
|
| 123 |
+
document.getElementById("cohereOutput").innerHTML = data.output; // Use innerHTML instead of textContent
|
| 124 |
+
});
|
| 125 |
+
</script>
|
| 126 |
+
</body>
|
| 127 |
+
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.97.*
|
| 2 |
+
requests==2.32.*
|
| 3 |
+
uvicorn[standard]==0.17.*
|
| 4 |
+
sentencepiece==0.1.*
|
| 5 |
+
torch==1.11.*
|
| 6 |
+
transformers==4.*
|
| 7 |
+
cohere==4.*
|