vjain commited on
Commit
81f8cdc
·
1 Parent(s): 5073e2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
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
- df = dataframes[dataframe_name]
40
- input = input
41
- input_vector = generate_embeddings(input, model_id,hf_token)
42
- df["similarities"]=df["embedding"].apply(lambda x: cosine_similarity([x],[input_vector])[0][0])
43
- data = df.sort_values("similarities", ascending=False).head(10)
44
- data.to_csv("sorted.csv")
45
- context = []
46
- for i, row in data.iterrows():
47
- context.append(row['text'])
48
- context
49
- text = "\n".join(context)
50
- context = text
51
- prompt = f"""
 
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
- return openai.Completion.create(
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