hamza50 commited on
Commit
35e8902
·
1 Parent(s): b2b5a46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -19
app.py CHANGED
@@ -32,6 +32,17 @@ def remove_html_tags(text):
32
 
33
  df['content'] = df.content.apply(lambda x: remove_html_tags(x))
34
  df['summary_html'] = df.summary_html.apply(lambda x: remove_html_tags(x))
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  def search(query):
37
  n = 10
@@ -43,19 +54,46 @@ def search(query):
43
  #results = results[['title','url','keywords','summary_html']].drop_duplicates()
44
  results = r_groupby.reset_index()
45
  results = results.sort_values("similarity", ascending=False)
46
- resultlist = []
 
47
  for r in results.index:
48
- resultlist.append(
49
- {
50
- "Title":results.title[r],
51
- "url":results.url[r],
52
- "score": str(results.similarity[r][0]),
53
- "summary": results.summary_html[r][:200],
54
- "keywords": results.keywords[r]
55
- }
56
- )
57
-
58
- return resultlist
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  def greet(query):
61
 
@@ -66,14 +104,10 @@ examples = [
66
  ["Climate Change Challenges in Europe"],
67
  ["Philosophy in the world of Minimalism"],
68
  ["Hate Speech vs Freedom of Speech"],
69
- ["Articles by Noam Chomsky on US Politics"],
70
  ["The importance of values and reflection"]
71
  ]
72
 
73
- demo = gr.Interface(fn=greet, title="cicero-semantic-search",
74
- inputs=gr.inputs.Textbox(lines=5, label="what would you like to learn about?"),
75
- outputs="json",examples=examples)
76
-
77
- demo.launch()
78
-
79
 
 
 
32
 
33
  df['content'] = df.content.apply(lambda x: remove_html_tags(x))
34
  df['summary_html'] = df.summary_html.apply(lambda x: remove_html_tags(x))
35
+ #testing new code
36
+ session_prompt = """ A bot that is open to discussions about different cultural, philosophical and political exchanges. I will use do different analysis to the articles provided to me. Stay truthful and if you weren't provided any resources give your oppinion only."""
37
+
38
+ def new_ask(user_input):
39
+ response = openai.ChatCompletion.create(model ="gpt-3.5-turbo",
40
+ messages = [{'role': 'system', 'content': session_prompt},{'role': 'user', 'content': user_input}],
41
+ temperature = 0
42
+
43
+ )
44
+ # print(response)
45
+ return response['choices'][0]['message']['content']
46
 
47
  def search(query):
48
  n = 10
 
54
  #results = results[['title','url','keywords','summary_html']].drop_duplicates()
55
  results = r_groupby.reset_index()
56
  results = results.sort_values("similarity", ascending=False)
57
+ tier_1 = []
58
+ tier_2 = []
59
  for r in results.index:
60
+
61
+ if results.similarity[r][0] > 0.5:
62
+
63
+ tier_1.append(
64
+ {
65
+ "title":results.title[r],
66
+ "url":results.url[r],
67
+ "score": str(results.similarity[r][0]),
68
+ "summary": results.summary_html[r][:200],
69
+ "keywords": results.keywords[r]
70
+ }
71
+ )
72
+
73
+ elif results.similarity[r][0] > 0.4:
74
+ tier_2.append(
75
+ {
76
+ "title":results.title[r],
77
+ "url":results.url[r],
78
+ "score": str(results.similarity[r][0]),
79
+ "summary": results.summary_html[r][:200],
80
+ "keywords": results.keywords[r]
81
+ }
82
+ )
83
+ print(tier_1)
84
+ print(tier_2)
85
+ ln = "\n"
86
+ prefix = f"tier 1:\n{ln.join([x['title'] for x in tier_1])}"
87
+ print(prefix)
88
+ answer = new_ask(f"Answer the following query by giving arguments from the different arguments provided below. Make sure to quote the article used if the argument corrseponds to the query: Query: {query} Articles {ln.join([x['title'] + ': ' + x['summary'] for i, x in enumerate(tier_1)])}\nUse careful reasoning to explain your answer and give your conclusion about this.")
89
+
90
+ if len(tier_2):
91
+ suffix = f"tier 2:\n{ln.join([x['title'] for x in tier_2])}"
92
+ related_questions = new_ask(f"Give general questions related the following articles: {ln.join([str(i) + ' ' + x['summary'] for i, x in enumerate(tier_2)])}")
93
+
94
+ return f"{answer}\n\nRelated Questions:\n{related_questions}"
95
+
96
+ return f"{answer}"
97
 
98
  def greet(query):
99
 
 
104
  ["Climate Change Challenges in Europe"],
105
  ["Philosophy in the world of Minimalism"],
106
  ["Hate Speech vs Freedom of Speech"],
 
107
  ["The importance of values and reflection"]
108
  ]
109
 
110
+ demo = gr.Interface(fn=greet, title="cicero-interactive-qa",
111
+ outputs = "text",inputs=gr.inputs.Textbox(lines=5, label="what would you like to learn about?"),examples=examples)
 
 
 
 
112
 
113
+ demo.launch(share = True, debug = True)