Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -36,26 +36,27 @@ dataframes = {
|
|
36 |
#df = pd.read_csv("TA_embeddings.csv")
|
37 |
#df["embedding"]=df["embedding"].apply(eval).apply(np.array)
|
38 |
def reply(input, dataframe_name):
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
Answer the following question using the context given below.If you don't know the answer for certain, say I don't know.
|
53 |
Context: {context}
|
54 |
|
55 |
Q: {input}
|
56 |
|
57 |
"""
|
58 |
-
|
59 |
prompt=prompt,
|
60 |
temperature=1,
|
61 |
max_tokens=500,
|
@@ -64,6 +65,10 @@ def reply(input, dataframe_name):
|
|
64 |
presence_penalty=0,
|
65 |
model="text-davinci-003"
|
66 |
)["choices"][0]["text"].strip(" \n")
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
|
|
|
36 |
#df = pd.read_csv("TA_embeddings.csv")
|
37 |
#df["embedding"]=df["embedding"].apply(eval).apply(np.array)
|
38 |
def reply(input, dataframe_name):
|
39 |
+
try:
|
40 |
+
df = dataframes[dataframe_name]
|
41 |
+
input = input
|
42 |
+
input_vector = generate_embeddings(input, model_id,hf_token)
|
43 |
+
df["similarities"]=df["embedding"].apply(lambda x: cosine_similarity([x],[input_vector])[0][0])
|
44 |
+
data = df.sort_values("similarities", ascending=False).head(10)
|
45 |
+
data.to_csv("sorted.csv")
|
46 |
+
context = []
|
47 |
+
for i, row in data.iterrows():
|
48 |
+
context.append(row['text'])
|
49 |
+
context
|
50 |
+
text = "\n".join(context)
|
51 |
+
context = text
|
52 |
+
prompt = f"""
|
53 |
Answer the following question using the context given below.If you don't know the answer for certain, say I don't know.
|
54 |
Context: {context}
|
55 |
|
56 |
Q: {input}
|
57 |
|
58 |
"""
|
59 |
+
response= openai.Completion.create(
|
60 |
prompt=prompt,
|
61 |
temperature=1,
|
62 |
max_tokens=500,
|
|
|
65 |
presence_penalty=0,
|
66 |
model="text-davinci-003"
|
67 |
)["choices"][0]["text"].strip(" \n")
|
68 |
+
return response
|
69 |
+
except Exception as e:
|
70 |
+
return f"An error occurred: {e}"
|
71 |
+
|
72 |
|
73 |
|
74 |
|