Francesco-A commited on
Commit
ac370a7
1 Parent(s): 084c343

new version

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -1,12 +1,11 @@
1
- # AUTOGENERATED! DO NOT EDIT! File to edit: ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb.
2
 
3
  # %% auto 0
4
  __all__ = ['s_model', 'qa_model', 'question_1', 'question_2', 'question_3', 'question_4', 'question_5', 'question_6',
5
- 'question_7', 'question_8', 'question_9', 'question_10', 'contexts', 'answer_example', 'a_text', 'n_slider',
6
- 'intf', 'QA_similarity']
7
 
8
-
9
- # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 2
10
  import pandas as pd
11
  import gradio as gr
12
 
@@ -16,7 +15,7 @@ s_model = SentenceTransformer('clips/mfaq')
16
  from transformers import pipeline
17
  qa_model = pipeline("question-answering")
18
 
19
- # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 3
20
  # Define the question(s)
21
  question_1 = "What are the main features of the new XPhone 20?"
22
  question_2 = "What are some benefits of regular exercise?"
@@ -73,12 +72,17 @@ contexts = [
73
  "The Declaration of Independence was adopted by the Continental Congress on July 4, 1776.",
74
  ]
75
 
76
- # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 5
77
  # Function to find similar answers in a list of contexts
78
  def QA_similarity(question, contexts, n_answers=1):
79
 
 
 
 
 
80
 
81
- contexts = context_df['Context'].to_list()
 
82
 
83
  # to use 'clips/mfaq' questions need to be prepended with <Q>, and answers with <A>.
84
  question = "<Q>"+question
@@ -112,35 +116,25 @@ def QA_similarity(question, contexts, n_answers=1):
112
 
113
  return df
114
 
115
- # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 7
116
-
117
  data = {
118
  'Context': contexts,
119
  }
120
 
121
  context_df = pd.DataFrame(data)
122
 
123
- a_text = gr.components.Dataframe(context_df,col_count=(1,"fixed"))
124
-
125
  n_slider = gr.components.Slider(minimum=1, maximum = 10, label = "Select n answers (max= 10)",step = 1)
126
 
127
  intf = gr.Interface(fn=QA_similarity,
128
  inputs= ["text", a_text, n_slider],
129
  outputs= gr.components.Dataframe(),
130
- examples=[
131
- [question_1],
132
- [question_2],
133
- [question_3],
134
- [question_4],
135
- [question_5],
136
- [question_6],
137
- [question_7],
138
- [question_8],
139
- [question_9],
140
- [question_10],],
141
  debug=True,
142
  )
143
 
144
- intf.launch(inline=False,
145
  # share=True
146
  )
 
1
+ # v1.1
2
 
3
  # %% auto 0
4
  __all__ = ['s_model', 'qa_model', 'question_1', 'question_2', 'question_3', 'question_4', 'question_5', 'question_6',
5
+ 'question_7', 'question_8', 'question_9', 'question_10', 'contexts', 'data', 'context_df', 'a_text',
6
+ 'n_slider', 'intf', 'QA_similarity']
7
 
8
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v2.ipynb 2
 
9
  import pandas as pd
10
  import gradio as gr
11
 
 
15
  from transformers import pipeline
16
  qa_model = pipeline("question-answering")
17
 
18
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v2.ipynb 3
19
  # Define the question(s)
20
  question_1 = "What are the main features of the new XPhone 20?"
21
  question_2 = "What are some benefits of regular exercise?"
 
72
  "The Declaration of Independence was adopted by the Continental Congress on July 4, 1776.",
73
  ]
74
 
75
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v2.ipynb 4
76
  # Function to find similar answers in a list of contexts
77
  def QA_similarity(question, contexts, n_answers=1):
78
 
79
+ if not isinstance(contexts, pd.DataFrame):
80
+ contexts = pd.read_csv(contexts)
81
+ contexts.rename(index={0: 'Context'})
82
+ contexts = contexts['Context'].to_list()
83
 
84
+ else:
85
+ contexts = contexts['Context'].to_list()
86
 
87
  # to use 'clips/mfaq' questions need to be prepended with <Q>, and answers with <A>.
88
  question = "<Q>"+question
 
116
 
117
  return df
118
 
119
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v2.ipynb 6
 
120
  data = {
121
  'Context': contexts,
122
  }
123
 
124
  context_df = pd.DataFrame(data)
125
 
126
+ a_text = gr.components.Dataframe(col_count=(1,"fixed"),interactive = True)
 
127
  n_slider = gr.components.Slider(minimum=1, maximum = 10, label = "Select n answers (max= 10)",step = 1)
128
 
129
  intf = gr.Interface(fn=QA_similarity,
130
  inputs= ["text", a_text, n_slider],
131
  outputs= gr.components.Dataframe(),
132
+ examples = [[question_1,context_df,3],
133
+ [question_2,context_df,5],
134
+ [question_4,context_df,10]],
 
 
 
 
 
 
 
 
135
  debug=True,
136
  )
137
 
138
+ intf.launch(inline=True,
139
  # share=True
140
  )