Spaces:
Build error
Build error
Artem Raichuk commited on
Commit ·
6a1d79e
1
Parent(s): 014ac6b
Add application file
Browse files- embeddings.csv +0 -0
- gradio_app.py +57 -0
- requirements.txt +62 -0
embeddings.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
gradio_app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import ast
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Get value OPEN_API_KEY
|
| 10 |
+
OPENAI_API_KEY = "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"
|
| 11 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 12 |
+
|
| 13 |
+
# Load embedding dataset
|
| 14 |
+
data = pd.read_csv("embeddings.csv")
|
| 15 |
+
data["embedding"] = data["embedding"].apply(ast.literal_eval)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def search_reviews(df_original, product_description, without_newlines=False, n=1):
|
| 19 |
+
df = df_original.copy()
|
| 20 |
+
if without_newlines:
|
| 21 |
+
product_description = product_description.replace("\n", " ")
|
| 22 |
+
|
| 23 |
+
embedding = (
|
| 24 |
+
client.embeddings.create(
|
| 25 |
+
input=[product_description], model="text-embedding-ada-002"
|
| 26 |
+
)
|
| 27 |
+
.data[0]
|
| 28 |
+
.embedding
|
| 29 |
+
)
|
| 30 |
+
df["similarities"] = df["embedding"].apply(
|
| 31 |
+
lambda x: cosine_similarity(
|
| 32 |
+
np.array(x).reshape(1, -1), np.array(embedding).reshape(1, -1)
|
| 33 |
+
)
|
| 34 |
+
)
|
| 35 |
+
res = df.sort_values("similarities", ascending=False).head(n)
|
| 36 |
+
return res.reset_index(drop=True)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def generate_response(text):
|
| 40 |
+
reference = search_reviews(data, text, without_newlines=False)["Content"][0]
|
| 41 |
+
|
| 42 |
+
completion = client.chat.completions.create(
|
| 43 |
+
model="gpt-3.5-turbo",
|
| 44 |
+
messages=[
|
| 45 |
+
{
|
| 46 |
+
"role": "system",
|
| 47 |
+
"content": f"Generate high-quality rewritten articles, ensuring logical composition, avoiding over-exaggeration, and refraining from any imaginative elements. Use the provided sample text as a reference for the desired writing style:{reference}",
|
| 48 |
+
},
|
| 49 |
+
{"role": "user", "content": f"{text}"},
|
| 50 |
+
],
|
| 51 |
+
)
|
| 52 |
+
result = completion.choices[0].message.content
|
| 53 |
+
return result
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
iface = gr.Interface(fn=generate_response, inputs="text", outputs="text")
|
| 57 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
annotated-types==0.6.0
|
| 2 |
+
anyio==3.7.1
|
| 3 |
+
appnope==0.1.3
|
| 4 |
+
asttokens==2.4.1
|
| 5 |
+
certifi==2023.11.17
|
| 6 |
+
comm==0.2.0
|
| 7 |
+
contourpy==1.2.0
|
| 8 |
+
cycler==0.12.1
|
| 9 |
+
debugpy==1.8.0
|
| 10 |
+
decorator==5.1.1
|
| 11 |
+
distro==1.8.0
|
| 12 |
+
dotenv==0.0.5
|
| 13 |
+
exceptiongroup==1.2.0
|
| 14 |
+
executing==2.0.1
|
| 15 |
+
fonttools==4.46.0
|
| 16 |
+
h11==0.14.0
|
| 17 |
+
httpcore==1.0.2
|
| 18 |
+
httpx==0.25.2
|
| 19 |
+
idna==3.6
|
| 20 |
+
ipykernel==6.27.1
|
| 21 |
+
ipython==8.18.1
|
| 22 |
+
jedi==0.19.1
|
| 23 |
+
joblib==1.3.2
|
| 24 |
+
jupyter_client==8.6.0
|
| 25 |
+
jupyter_core==5.5.0
|
| 26 |
+
kiwisolver==1.4.5
|
| 27 |
+
matplotlib==3.8.2
|
| 28 |
+
matplotlib-inline==0.1.6
|
| 29 |
+
nest-asyncio==1.5.8
|
| 30 |
+
numpy==1.26.2
|
| 31 |
+
openai==1.3.7
|
| 32 |
+
packaging==23.2
|
| 33 |
+
pandas==2.1.3
|
| 34 |
+
parso==0.8.3
|
| 35 |
+
pexpect==4.9.0
|
| 36 |
+
Pillow==10.1.0
|
| 37 |
+
platformdirs==4.1.0
|
| 38 |
+
prompt-toolkit==3.0.41
|
| 39 |
+
psutil==5.9.6
|
| 40 |
+
psycopg2-binary==2.9.9
|
| 41 |
+
ptyprocess==0.7.0
|
| 42 |
+
pure-eval==0.2.2
|
| 43 |
+
pydantic==2.5.2
|
| 44 |
+
pydantic_core==2.14.5
|
| 45 |
+
Pygments==2.17.2
|
| 46 |
+
pyparsing==3.1.1
|
| 47 |
+
python-dateutil==2.8.2
|
| 48 |
+
python-dotenv==1.0.0
|
| 49 |
+
pytz==2023.3.post1
|
| 50 |
+
pyzmq==25.1.2
|
| 51 |
+
scikit-learn==1.3.2
|
| 52 |
+
scipy==1.11.4
|
| 53 |
+
six==1.16.0
|
| 54 |
+
sniffio==1.3.0
|
| 55 |
+
stack-data==0.6.3
|
| 56 |
+
threadpoolctl==3.2.0
|
| 57 |
+
tornado==6.4
|
| 58 |
+
tqdm==4.66.1
|
| 59 |
+
traitlets==5.14.0
|
| 60 |
+
typing_extensions==4.8.0
|
| 61 |
+
tzdata==2023.3
|
| 62 |
+
wcwidth==0.2.12
|