| | |
| | import gradio as gr |
| | import pandas as pd |
| | from sentence_transformers import SentenceTransformer |
| | from sklearn.metrics.pairwise import cosine_similarity |
| |
|
| | |
| | model = SentenceTransformer('all-MiniLM-L6-v2') |
| |
|
| | |
| | def find_matching_notes(uploaded_file, user_input): |
| | |
| | if uploaded_file is not None: |
| | df = pd.read_csv(uploaded_file.name) |
| | |
| | |
| | if not {'Source', 'Section', 'Notes'}.issubset(df.columns): |
| | return "The uploaded file must contain 'Source', 'Section', and 'Notes' columns." |
| | |
| | |
| | if df[['Notes', 'Section']].isnull().any().any(): |
| | |
| | df['Notes'].fillna('', inplace=True) |
| | df['Section'].fillna('', inplace=True) |
| | |
| | |
| | df['Combined'] = df['Notes'] + ' ' + df['Section'] |
| | |
| | |
| | all_texts = df['Combined'].tolist() + [user_input] |
| | embeddings = model.encode(all_texts, convert_to_tensor=True) |
| | |
| | |
| | cosine_similarities = cosine_similarity(embeddings[-1].unsqueeze(0), embeddings[:-1]) |
| | |
| | |
| | top_indices = cosine_similarities[0].argsort()[-5:][::-1] |
| | |
| | |
| | results = df.iloc[top_indices][['Notes', 'Source', 'Section']] |
| | formatted_results = [] |
| | |
| | for index, row in results.iterrows(): |
| | formatted_results.append( |
| | f"**Notes:** {row['Notes']}\n" |
| | f"**Source:** {row['Source']}\n" |
| | f"**Section:** {row['Section']}\n" |
| | "-------------------------------------\n" |
| | ) |
| | |
| | return "\n".join(formatted_results) |
| | |
| | return "Please upload a valid CSV file." |
| |
|
| | |
| | iface = gr.Interface( |
| | fn=find_matching_notes, |
| | inputs=[ |
| | gr.File(label="Upload Research Notes (CSV)"), |
| | gr.Textbox(label="Enter your text here", placeholder="Type your content...") |
| | ], |
| | outputs=gr.Textbox(label="Top 5 Matching Entries", lines=15, placeholder="Results will be displayed here..."), |
| | title="Research Notes Matcher", |
| | description="Upload a CSV file with 'Source', 'Section', and 'Notes'. Enter your text to find the top 5 matching notes. For documentation check the Readme file in the files section" |
| | ) |
| |
|
| | |
| | iface.launch() |
| |
|