Update app.py
Browse files
app.py
CHANGED
|
@@ -4,71 +4,71 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 4 |
from sklearn.metrics.pairwise import linear_kernel
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# Function to fetch
|
| 8 |
-
def
|
| 9 |
-
url = "https://
|
| 10 |
params = {
|
| 11 |
-
'search_terms': '
|
| 12 |
-
'page_size': 100,
|
| 13 |
'json': True
|
| 14 |
}
|
| 15 |
|
| 16 |
response = requests.get(url, params=params)
|
| 17 |
if response.status_code == 200:
|
| 18 |
data = response.json()
|
| 19 |
-
|
| 20 |
|
| 21 |
-
# Create a DataFrame from the
|
| 22 |
-
|
| 23 |
-
for
|
| 24 |
-
|
| 25 |
-
'
|
| 26 |
-
'
|
| 27 |
-
'
|
| 28 |
-
'
|
| 29 |
-
'
|
| 30 |
}
|
| 31 |
-
|
| 32 |
|
| 33 |
-
return pd.DataFrame(
|
| 34 |
else:
|
| 35 |
print("Failed to fetch data:", response.status_code)
|
| 36 |
return pd.DataFrame()
|
| 37 |
|
| 38 |
-
# Fetch the
|
| 39 |
-
|
| 40 |
|
| 41 |
-
#
|
| 42 |
tfidf = TfidfVectorizer(stop_words='english')
|
| 43 |
-
tfidf_matrix = tfidf.fit_transform(
|
| 44 |
|
| 45 |
# Compute the cosine similarity matrix
|
| 46 |
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
|
| 47 |
|
| 48 |
-
# Function to get recommendations based on
|
| 49 |
-
def
|
| 50 |
-
if
|
| 51 |
-
return pd.DataFrame(columns=['
|
| 52 |
-
idx =
|
| 53 |
sim_scores = list(enumerate(cosine_sim[idx]))
|
| 54 |
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
| 55 |
-
sim_scores = sim_scores[1:4] # Get top 3
|
| 56 |
-
|
| 57 |
-
return
|
| 58 |
|
| 59 |
-
# Define the
|
| 60 |
-
def
|
| 61 |
-
|
| 62 |
-
return
|
| 63 |
|
| 64 |
# Create the Gradio interface
|
| 65 |
-
|
| 66 |
interface = gr.Interface(
|
| 67 |
-
fn=
|
| 68 |
-
inputs=gr.Dropdown(choices=
|
| 69 |
outputs="dataframe",
|
| 70 |
-
title="
|
| 71 |
-
description="Select a
|
| 72 |
)
|
| 73 |
|
| 74 |
# Launch the interface
|
|
|
|
| 4 |
from sklearn.metrics.pairwise import linear_kernel
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# Function to fetch medicine data from Open Drug Facts (or another source if available)
|
| 8 |
+
def fetch_medicine_data():
|
| 9 |
+
url = "https://api.opendrugfacts.org/cgi/search.pl" # Replace with actual URL if available
|
| 10 |
params = {
|
| 11 |
+
'search_terms': 'drug', # General search term for medications
|
| 12 |
+
'page_size': 100, # Fetch up to 100 drugs
|
| 13 |
'json': True
|
| 14 |
}
|
| 15 |
|
| 16 |
response = requests.get(url, params=params)
|
| 17 |
if response.status_code == 200:
|
| 18 |
data = response.json()
|
| 19 |
+
medicines = data.get('products', [])
|
| 20 |
|
| 21 |
+
# Create a DataFrame from the medicine data
|
| 22 |
+
medicine_data = []
|
| 23 |
+
for med in medicines:
|
| 24 |
+
med_info = {
|
| 25 |
+
'drug_id': med.get('id'),
|
| 26 |
+
'drug_name': med.get('product_name'),
|
| 27 |
+
'composition': med.get('ingredients_text', 'No composition available'),
|
| 28 |
+
'category': med.get('categories', ['Unknown'])[0],
|
| 29 |
+
'description': med.get('generic_name', 'No description available')
|
| 30 |
}
|
| 31 |
+
medicine_data.append(med_info)
|
| 32 |
|
| 33 |
+
return pd.DataFrame(medicine_data)
|
| 34 |
else:
|
| 35 |
print("Failed to fetch data:", response.status_code)
|
| 36 |
return pd.DataFrame()
|
| 37 |
|
| 38 |
+
# Fetch the medicine data
|
| 39 |
+
medicines_df = fetch_medicine_data()
|
| 40 |
|
| 41 |
+
# Create the TF-IDF matrix for finding similar medicines
|
| 42 |
tfidf = TfidfVectorizer(stop_words='english')
|
| 43 |
+
tfidf_matrix = tfidf.fit_transform(medicines_df['composition'].fillna(''))
|
| 44 |
|
| 45 |
# Compute the cosine similarity matrix
|
| 46 |
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
|
| 47 |
|
| 48 |
+
# Function to get recommendations based on drug name or composition
|
| 49 |
+
def get_alternatives(drug_name, cosine_sim=cosine_sim):
|
| 50 |
+
if drug_name not in medicines_df['drug_name'].values:
|
| 51 |
+
return pd.DataFrame(columns=['drug_name', 'composition']) # Return empty DataFrame if not found
|
| 52 |
+
idx = medicines_df.index[medicines_df['drug_name'] == drug_name][0]
|
| 53 |
sim_scores = list(enumerate(cosine_sim[idx]))
|
| 54 |
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
|
| 55 |
+
sim_scores = sim_scores[1:4] # Get top 3 alternatives
|
| 56 |
+
alternative_indices = [i[0] for i in sim_scores]
|
| 57 |
+
return medicines_df.iloc[alternative_indices][['drug_name', 'composition']]
|
| 58 |
|
| 59 |
+
# Define the recommend_alternative function for Gradio
|
| 60 |
+
def recommend_alternative(selected_drug):
|
| 61 |
+
alternatives = get_alternatives(selected_drug)
|
| 62 |
+
return alternatives
|
| 63 |
|
| 64 |
# Create the Gradio interface
|
| 65 |
+
drug_names = medicines_df['drug_name'].dropna().tolist() # List of drug names
|
| 66 |
interface = gr.Interface(
|
| 67 |
+
fn=recommend_alternative,
|
| 68 |
+
inputs=gr.Dropdown(choices=drug_names, label="Select a Drug"),
|
| 69 |
outputs="dataframe",
|
| 70 |
+
title="Medicine Alternative Recommendation System",
|
| 71 |
+
description="Select a medicine to see alternative drugs with similar compositions."
|
| 72 |
)
|
| 73 |
|
| 74 |
# Launch the interface
|