first trial app
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
|
| 6 |
+
# Function to process the uploaded file and find top 5 matching notes
|
| 7 |
+
def find_matching_notes(uploaded_file, user_input):
|
| 8 |
+
# Read the uploaded CSV file
|
| 9 |
+
if uploaded_file is not None:
|
| 10 |
+
df = pd.read_csv(uploaded_file.name)
|
| 11 |
+
|
| 12 |
+
# Ensure the necessary columns are present
|
| 13 |
+
if not {'Source', 'Section', 'Notes'}.issubset(df.columns):
|
| 14 |
+
return "The uploaded file must contain 'Source', 'Section', and 'Notes' columns."
|
| 15 |
+
|
| 16 |
+
# Combine 'Notes' and 'Section' for processing
|
| 17 |
+
df['Combined'] = df['Notes'] + ' ' + df['Section']
|
| 18 |
+
|
| 19 |
+
# Create TF-IDF vectorizer and transform the texts
|
| 20 |
+
vectorizer = TfidfVectorizer()
|
| 21 |
+
all_texts = df['Combined'].tolist() + [user_input]
|
| 22 |
+
tfidf_matrix = vectorizer.fit_transform(all_texts)
|
| 23 |
+
|
| 24 |
+
# Compute cosine similarity
|
| 25 |
+
cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
|
| 26 |
+
|
| 27 |
+
# Get the top 5 indices of the most similar entries
|
| 28 |
+
top_indices = cosine_similarities[0].argsort()[-5:][::-1]
|
| 29 |
+
|
| 30 |
+
# Prepare the results
|
| 31 |
+
results = df.iloc[top_indices][['Notes', 'Source', 'Section']]
|
| 32 |
+
results_list = results.values.tolist()
|
| 33 |
+
|
| 34 |
+
return results_list
|
| 35 |
+
return "Please upload a valid CSV file."
|
| 36 |
+
|
| 37 |
+
# Create Gradio interface
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=find_matching_notes,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.File(label="Upload Research Notes (CSV)"),
|
| 42 |
+
gr.Textbox(label="Enter your text here", placeholder="Type your content...")
|
| 43 |
+
],
|
| 44 |
+
outputs=gr.Dataframe(headers=["Notes", "Source", "Section"], label="Top 5 Matching Entries"),
|
| 45 |
+
title="Research Notes Matcher",
|
| 46 |
+
description="Upload a CSV file with 'Source', 'Section', and 'Notes'. Enter your text to find the top 5 matching notes."
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Launch the app
|
| 50 |
+
iface.launch()
|