Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
|
|
| 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 = "
|
| 11 |
-
|
|
|
|
| 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()
|
|
@@ -35,12 +50,13 @@ def search_reviews(df_original, product_description, without_newlines=False, n=1
|
|
| 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=
|
|
|
|
|
|
|
| 44 |
messages=[
|
| 45 |
{
|
| 46 |
"role": "system",
|
|
@@ -52,6 +68,10 @@ def generate_response(text):
|
|
| 52 |
result = completion.choices[0].message.content
|
| 53 |
return result
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
iface.launch(auth=(
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import ast
|
| 5 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
import numpy as np
|
| 7 |
+
from openai import OpenAI
|
| 8 |
|
| 9 |
# Get value OPEN_API_KEY
|
| 10 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 11 |
+
login = os.environ.get("login")
|
| 12 |
+
password = os.environ.get("password")
|
| 13 |
|
| 14 |
# Load embedding dataset
|
| 15 |
data = pd.read_csv("embeddings.csv")
|
| 16 |
data["embedding"] = data["embedding"].apply(ast.literal_eval)
|
| 17 |
|
| 18 |
+
# Initialize Gradio Interface
|
| 19 |
+
iface = gr.Interface()
|
| 20 |
+
|
| 21 |
+
# Dropdown for selecting the model
|
| 22 |
+
model_dropdown = gr.DropDown(models=["gpt-3.5-turbo", "gpt-4-1106-preview"], label="Select Model")
|
| 23 |
+
iface.add(model_dropdown, "model")
|
| 24 |
+
|
| 25 |
+
# Slider for temperature
|
| 26 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, default=0.5, label="Temperature")
|
| 27 |
+
iface.add(temperature_slider, "temperature")
|
| 28 |
+
|
| 29 |
+
# Slider for top_p
|
| 30 |
+
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, default=0.5, label="Top P")
|
| 31 |
+
iface.add(top_p_slider, "top_p")
|
| 32 |
|
| 33 |
def search_reviews(df_original, product_description, without_newlines=False, n=1):
|
| 34 |
df = df_original.copy()
|
|
|
|
| 50 |
res = df.sort_values("similarities", ascending=False).head(n)
|
| 51 |
return res.reset_index(drop=True)
|
| 52 |
|
| 53 |
+
def generate_response(text, model, temperature, top_p):
|
|
|
|
| 54 |
reference = search_reviews(data, text, without_newlines=False)["Content"][0]
|
| 55 |
|
| 56 |
completion = client.chat.completions.create(
|
| 57 |
+
model=model,
|
| 58 |
+
temperature=temperature,
|
| 59 |
+
top_p=top_p,
|
| 60 |
messages=[
|
| 61 |
{
|
| 62 |
"role": "system",
|
|
|
|
| 68 |
result = completion.choices[0].message.content
|
| 69 |
return result
|
| 70 |
|
| 71 |
+
# Set the function for Gradio Interface
|
| 72 |
+
iface.fn = generate_response
|
| 73 |
+
iface.inputs = ["text", "model", "temperature", "top_p"]
|
| 74 |
+
iface.outputs = "text"
|
| 75 |
|
| 76 |
+
# Launch the Gradio Interface
|
| 77 |
+
iface.launch(auth=(login, password))
|