akazmi commited on
Commit
d919c6b
·
verified ·
1 Parent(s): b0bf781

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -38
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 sustainable products from Open Food Facts API
8
- def fetch_sustainable_products():
9
- url = "https://world.openfoodfacts.org/cgi/search.pl"
10
  params = {
11
- 'search_terms': 'sustainable',
12
- 'page_size': 100, # Fetch up to 100 products
13
  'json': True
14
  }
15
 
16
  response = requests.get(url, params=params)
17
  if response.status_code == 200:
18
  data = response.json()
19
- products = data.get('products', [])
20
 
21
- # Create a DataFrame from the product data
22
- product_data = []
23
- for product in products:
24
- product_info = {
25
- 'product_id': product.get('id'),
26
- 'product_name': product.get('product_name'),
27
- 'category': product.get('categories', ['Unknown'])[0], # Get first category or 'Unknown'
28
- 'description': product.get('ingredients_text', 'No description available'),
29
- 'eco_rating': product.get('ecoscore_score', 0) # Eco score (if available)
30
  }
31
- product_data.append(product_info)
32
 
33
- return pd.DataFrame(product_data)
34
  else:
35
  print("Failed to fetch data:", response.status_code)
36
  return pd.DataFrame()
37
 
38
- # Fetch the sustainable products
39
- products_df = fetch_sustainable_products()
40
 
41
- # Creating the TF-IDF matrix
42
  tfidf = TfidfVectorizer(stop_words='english')
43
- tfidf_matrix = tfidf.fit_transform(products_df['description'].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 product name
49
- def get_recommendations(product_name, cosine_sim=cosine_sim):
50
- if product_name not in products_df['product_name'].values:
51
- return pd.DataFrame(columns=['product_name', 'description']) # Return empty DataFrame if not found
52
- idx = products_df.index[products_df['product_name'] == product_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 recommendations
56
- product_indices = [i[0] for i in sim_scores]
57
- return products_df.iloc[product_indices][['product_name', 'description']]
58
 
59
- # Define the recommend_product function for Gradio
60
- def recommend_product(selected_product):
61
- recommendations = get_recommendations(selected_product)
62
- return recommendations
63
 
64
  # Create the Gradio interface
65
- product_names = products_df['product_name'].dropna().tolist() # List of product names
66
  interface = gr.Interface(
67
- fn=recommend_product,
68
- inputs=gr.Dropdown(choices=product_names, label="Select a Product"),
69
  outputs="dataframe",
70
- title="Sustainable Product Recommendation System",
71
- description="Select a product to see similar sustainable product recommendations."
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