Spaces:
Running
Running
DCComplex
commited on
Commit
·
e7739fb
1
Parent(s):
2dbfc5b
Add application file
Browse files- Dockerfile +16 -0
- app.py +74 -0
- requirements.txt +5 -0
- templates/index.html +89 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
RUN useradd -m -u 1000 user
|
7 |
+
USER user
|
8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
9 |
+
|
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 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from flask import Flask, render_template, request, jsonify
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqGeneration
|
4 |
+
import torch
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Load the model and tokenizer
|
9 |
+
model_name = "t5-base" # T5 is better suited for paraphrasing
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForSeq2SeqGeneration.from_pretrained(model_name)
|
12 |
+
|
13 |
+
def paraphrase_text(text, num_variants=3, max_length=100):
|
14 |
+
"""
|
15 |
+
Paraphrase the input text using T5 model
|
16 |
+
"""
|
17 |
+
if not text.strip():
|
18 |
+
return []
|
19 |
+
|
20 |
+
# Prepare the input text
|
21 |
+
prefix = "paraphrase: "
|
22 |
+
input_text = prefix + text
|
23 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=max_length, truncation=True)
|
24 |
+
|
25 |
+
# Generate paraphrases
|
26 |
+
outputs = model.generate(
|
27 |
+
input_ids,
|
28 |
+
num_return_sequences=num_variants,
|
29 |
+
num_beams=num_variants * 2,
|
30 |
+
max_length=max_length,
|
31 |
+
temperature=0.7,
|
32 |
+
top_k=50,
|
33 |
+
top_p=0.95,
|
34 |
+
do_sample=True,
|
35 |
+
repetition_penalty=1.2,
|
36 |
+
early_stopping=True
|
37 |
+
)
|
38 |
+
|
39 |
+
# Decode and return the generated paraphrases
|
40 |
+
paraphrases = []
|
41 |
+
for output in outputs:
|
42 |
+
paraphrased = tokenizer.decode(output, skip_special_tokens=True)
|
43 |
+
if paraphrased.lower() != text.lower(): # Avoid exact duplicates
|
44 |
+
paraphrases.append(paraphrased)
|
45 |
+
|
46 |
+
return paraphrases
|
47 |
+
|
48 |
+
@app.route("/", methods=["GET", "POST"])
|
49 |
+
def index():
|
50 |
+
paraphrased_texts = []
|
51 |
+
original_text = ""
|
52 |
+
error_message = ""
|
53 |
+
|
54 |
+
if request.method == "POST":
|
55 |
+
original_text = request.form.get("text", "").strip()
|
56 |
+
if not original_text:
|
57 |
+
error_message = "Please enter some text to paraphrase."
|
58 |
+
else:
|
59 |
+
try:
|
60 |
+
paraphrased_texts = paraphrase_text(original_text)
|
61 |
+
if not paraphrased_texts:
|
62 |
+
error_message = "Could not generate paraphrases. Please try different text."
|
63 |
+
except Exception as e:
|
64 |
+
error_message = f"An error occurred: {str(e)}"
|
65 |
+
|
66 |
+
return render_template(
|
67 |
+
"index.html",
|
68 |
+
paraphrased_texts=paraphrased_texts,
|
69 |
+
original_text=original_text,
|
70 |
+
error_message=error_message
|
71 |
+
)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
flask
|
4 |
+
torch
|
5 |
+
transformers
|
templates/index.html
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>AI Paraphrasing Tool</title>
|
8 |
+
<style>
|
9 |
+
body {
|
10 |
+
font-family: Arial, sans-serif;
|
11 |
+
max-width: 800px;
|
12 |
+
margin: 0 auto;
|
13 |
+
padding: 20px;
|
14 |
+
background-color: #f5f5f5;
|
15 |
+
}
|
16 |
+
.container {
|
17 |
+
background-color: white;
|
18 |
+
padding: 20px;
|
19 |
+
border-radius: 8px;
|
20 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
21 |
+
}
|
22 |
+
textarea {
|
23 |
+
width: 100%;
|
24 |
+
min-height: 100px;
|
25 |
+
margin: 10px 0;
|
26 |
+
padding: 10px;
|
27 |
+
border: 1px solid #ddd;
|
28 |
+
border-radius: 4px;
|
29 |
+
resize: vertical;
|
30 |
+
}
|
31 |
+
button {
|
32 |
+
background-color: #007bff;
|
33 |
+
color: white;
|
34 |
+
padding: 10px 20px;
|
35 |
+
border: none;
|
36 |
+
border-radius: 4px;
|
37 |
+
cursor: pointer;
|
38 |
+
}
|
39 |
+
button:hover {
|
40 |
+
background-color: #0056b3;
|
41 |
+
}
|
42 |
+
.error {
|
43 |
+
color: #dc3545;
|
44 |
+
margin: 10px 0;
|
45 |
+
}
|
46 |
+
.result {
|
47 |
+
margin-top: 20px;
|
48 |
+
padding: 15px;
|
49 |
+
background-color: #f8f9fa;
|
50 |
+
border-radius: 4px;
|
51 |
+
}
|
52 |
+
.variant {
|
53 |
+
margin: 10px 0;
|
54 |
+
padding: 10px;
|
55 |
+
background-color: #e9ecef;
|
56 |
+
border-radius: 4px;
|
57 |
+
}
|
58 |
+
</style>
|
59 |
+
</head>
|
60 |
+
<body>
|
61 |
+
<div class="container">
|
62 |
+
<h1>AI Paraphrasing Tool</h1>
|
63 |
+
<form method="POST">
|
64 |
+
<div>
|
65 |
+
<label for="text">Enter text to paraphrase:</label>
|
66 |
+
<textarea name="text" id="text" required>{{ original_text }}</textarea>
|
67 |
+
</div>
|
68 |
+
<button type="submit">Generate Paraphrases</button>
|
69 |
+
</form>
|
70 |
+
|
71 |
+
{% if error_message %}
|
72 |
+
<div class="error">
|
73 |
+
{{ error_message }}
|
74 |
+
</div>
|
75 |
+
{% endif %}
|
76 |
+
|
77 |
+
{% if paraphrased_texts %}
|
78 |
+
<div class="result">
|
79 |
+
<h3>Paraphrased Versions:</h3>
|
80 |
+
{% for text in paraphrased_texts %}
|
81 |
+
<div class="variant">
|
82 |
+
{{ text }}
|
83 |
+
</div>
|
84 |
+
{% endfor %}
|
85 |
+
</div>
|
86 |
+
{% endif %}
|
87 |
+
</div>
|
88 |
+
</body>
|
89 |
+
</html>
|