Francesco-A commited on
Commit
1d026b1
1 Parent(s): 69cfe23

initial commit

Browse files
Files changed (2) hide show
  1. app.py +142 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 1
9
+ !pip install -U sentence-transformers
10
+ !pip install gradio
11
+
12
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 2
13
+ import pandas as pd
14
+ import gradio as gr
15
+
16
+ from sentence_transformers import SentenceTransformer, util
17
+ s_model = SentenceTransformer('clips/mfaq')
18
+
19
+ from transformers import pipeline
20
+ qa_model = pipeline("question-answering")
21
+
22
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 3
23
+ # Define the question(s)
24
+ question_1 = "What are the main features of the new XPhone 20?"
25
+ question_2 = "What are some benefits of regular exercise?"
26
+ question_3 = "What is the color of a rose?"
27
+ question_4 = "How does photosynthesis work in plants?"
28
+ question_5 = "At what temperature does water boil?"
29
+ question_6 = "Where can I find potassium?"
30
+ question_7 = "How does the internet function?"
31
+ question_8 = "What are the ingredients for making a classic margarita?"
32
+ question_9 = "How does cellular respiration work?"
33
+ question_10 = "Is money important?"
34
+
35
+ # Define the contexts as a list of strings
36
+ contexts = [
37
+ "The XPhone 20 is expected to come with an improved camera system, featuring advanced image stabilization and enhanced low-light capabilities.",
38
+ "Regular exercise has been shown to reduce the risk of chronic diseases such as heart disease, diabetes, and certain types of cancer.",
39
+ "Roses come in various colors, including red, pink, yellow, white, and even blue (though blue roses are rare and often created through genetic modification).",
40
+ "Photosynthesis occurs in the chloroplasts of plant cells, where chlorophyll captures sunlight and converts it into chemical energy.",
41
+ "Water boils at different temperatures depending on factors like altitude and atmospheric pressure. At sea level, it boils at 100 degrees Celsius or 212 degrees Fahrenheit.",
42
+ "Potassium is an essential mineral that can be found in various foods such as bananas, potatoes, spinach, and oranges.",
43
+ "The internet functions through a complex system of data transmission protocols, routers, and servers that allow for the exchange of information globally.",
44
+ "A classic margarita typically consists of tequila, lime juice, triple sec (or orange liqueur), and is often served with a salted rim.",
45
+ "Cellular respiration takes place in the mitochondria of cells, where glucose and oxygen are converted into ATP (adenosine triphosphate) and carbon dioxide.",
46
+ "Money is a medium of exchange that facilitates transactions of goods and services. Its importance lies in its role in economic systems and the ability to represent value.",
47
+ "The XPhone 20 may feature an OLED display for vibrant colors and deep blacks, providing a high-quality visual experience.",
48
+ "Exercise releases endorphins, which are chemicals in the brain that help improve mood and reduce feelings of stress and anxiety.",
49
+ "Different species of roses can have variations in color, including shades of red, pink, yellow, and white.",
50
+ "During photosynthesis, plants also release oxygen as a byproduct, which is essential for the survival of many organisms on Earth.",
51
+ "Water boils at a lower temperature at higher altitudes due to the reduced atmospheric pressure. For example, in the mountains, it may boil below 100 degrees Celsius.",
52
+ "Potassium is crucial for proper muscle function, nerve function, and maintaining fluid balance in the body.",
53
+ "The internet relies on a system of interconnected networks, including wired and wireless connections, to transmit data across the globe.",
54
+ "In addition to the core ingredients, a classic margarita can also be garnished with a wedge of lime for added flavor.",
55
+ "Cellular respiration involves several stages, including glycolysis, the Krebs cycle, and the electron transport chain, to extract energy from glucose.",
56
+ "Money serves as a unit of account, allowing for standardized pricing and valuation of goods and services in economies worldwide."
57
+ "The XPhone 20 is rumored to feature a smaller notch, providing more screen real estate for users. This allows for an immersive viewing experience.",
58
+ "Photosynthesis is the process by which plants convert carbon dioxide, water, and sunlight into glucose and oxygen.",
59
+ "Bananas are a great source of potassium.",
60
+ "The theory of relativity was developed by Albert Einstein and revolutionized our understanding of space and time.",
61
+ "The Eiffel Tower is located in Paris, France.",
62
+ "Reports suggest that the XPhone 20 will have significant improvements in battery life compared to its predecessor. Users can expect a longer-lasting device.",
63
+ "A penny saved is a penny earned.",
64
+ "Water boils at 100 degrees Celsius.",
65
+ "The Great Wall of China is one of the most impressive architectural feats in history.",
66
+ "The capital of Japan is Tokyo.",
67
+ "One of the anticipated features of the XPhone 20 is a faster and more powerful A16 chip. This will result in smoother and more efficient performance.",
68
+ "Roses are red, violets are blue.",
69
+ "Regular exercise can help improve cardiovascular health and strengthen muscles.",
70
+ "A classic margarita is made with tequila, lime juice, and orange liqueur.",
71
+ "Cellular respiration is the process by which cells convert glucose and oxygen into energy, carbon dioxide, and water.",
72
+ "The internet is a global network of interconnected computers and servers that allows the sharing of information and resources.",
73
+ "Mount Everest is the highest mountain in the world, located in the Himalayas.",
74
+ "The sun rises in the east and sets in the west.",
75
+ "The Mona Lisa is a famous portrait painting by Leonardo da Vinci.",
76
+ "The Declaration of Independence was adopted by the Continental Congress on July 4, 1776.",
77
+ ]
78
+
79
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 5
80
+ # Function to find similar answers in a list of contexts
81
+ def QA_similarity(question, contexts, n_answers=1):
82
+
83
+ contexts = contexts.iloc[:,0].to_list()
84
+
85
+ # to use 'clips/mfaq' questions need to be prepended with <Q>, and answers with <A>.
86
+ question = "<Q>"+question
87
+ mfaq_contexts = ["<A>"+answer for answer in contexts]
88
+
89
+ # Import the necessary module for itemgetter
90
+ from operator import itemgetter
91
+
92
+ # Create context embedding
93
+ query_embedding = s_model.encode(question)
94
+ context_embeddings = s_model.encode(mfaq_contexts)
95
+
96
+ # Perform semantic search to find similar contexts
97
+ data = util.semantic_search(query_embedding, context_embeddings)[0]
98
+
99
+ # Sort the list of dictionaries by the 'score' key in descending order
100
+ sorted_answers = sorted(data, key=itemgetter('score'), reverse=True)
101
+
102
+ # Extract answer, context and score
103
+ answer_ids = [item['corpus_id'] for item in sorted_answers]
104
+ context_score = [item['score'] for item in sorted_answers]
105
+ answers_scores = [qa_model(question, contexts[n]) for n in answer_ids[:n_answers]]
106
+
107
+ # Extract relevant information for answers and scores
108
+ answer_context = [contexts[n] for n in answer_ids]
109
+ answers = [a['answer'] for a in answers_scores]
110
+ scores = [s['score'] for s in answers_scores]
111
+
112
+ # Create a DataFrame for the results
113
+ df = pd.DataFrame(zip(answer_ids, answers, context_score, answer_context), columns=["ANSWER_IDX", "ANSWER", "CONTEXT_SCORE", "CONTEXT"])
114
+
115
+ return df
116
+
117
+ # %% ../drive/MyDrive/Codici/Python/Gradio_App/SemanticSearch_QA-v1.ipynb 7
118
+ answer_example = pd.DataFrame(contexts, columns=['Context'])
119
+ a_text = gr.components.Dataframe(answer_example,col_count=(1,"fixed"))
120
+
121
+ n_slider = gr.components.Slider(minimum=1, maximum = 10, label = "Select n answers (max= 10)",step = 1)
122
+
123
+ intf = gr.Interface(fn=QA_similarity,
124
+ inputs= ["text", a_text, n_slider],
125
+ outputs= gr.components.Dataframe(),
126
+ examples=[
127
+ [question_1],
128
+ [question_2],
129
+ [question_3],
130
+ [question_4],
131
+ [question_5],
132
+ [question_6],
133
+ [question_7],
134
+ [question_8],
135
+ [question_9],
136
+ [question_10],],
137
+ debug=True,
138
+ )
139
+
140
+ intf.launch(inline=False,
141
+ # share=True
142
+ )
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ sentence-transformers==2.2.2
2
+ gradio==3.44.3
3
+ gradio_client==0.5.0