devbernie commited on
Commit
0f95a2e
·
verified ·
1 Parent(s): 9aece72

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import pandas as pd
3
+ from fuzzywuzzy import process
4
+ from sklearn.feature_extraction.text import TfidfVectorizer
5
+ from sklearn.metrics.pairwise import cosine_similarity
6
+ import gradio as gr
7
+
8
+ # Load dataset from GitHub
9
+ url = "https://raw.githubusercontent.com/devbernie/Medimatch/refs/heads/main/dataset.csv"
10
+ data = pd.read_csv(url)
11
+
12
+ # Preprocess data
13
+ columns_to_process = ['Composition', 'Uses', 'Medicine Name']
14
+ for column in columns_to_process:
15
+ data[column] = data[column].fillna('').str.lower()
16
+
17
+ # Fuzzy search function
18
+ def fuzzy_search(query, data_column, threshold=80):
19
+ matches = process.extract(query.lower(), data_column, limit=5)
20
+ results = [match[0] for match in matches if match[1] >= threshold]
21
+ return results
22
+
23
+ # Vectorize data
24
+ vectorizer_composition = TfidfVectorizer()
25
+ composition_matrix = vectorizer_composition.fit_transform(data['Composition'])
26
+
27
+ vectorizer_uses = TfidfVectorizer()
28
+ uses_matrix = vectorizer_uses.fit_transform(data['Uses'])
29
+
30
+ # Calculate similarities
31
+ composition_similarity = cosine_similarity(composition_matrix)
32
+ uses_similarity = cosine_similarity(uses_matrix)
33
+ overall_similarity = (composition_similarity + uses_similarity) / 2
34
+
35
+ # Recommendation function
36
+ def recommend_medicine(input_name, top_n=5):
37
+ matches = fuzzy_search(input_name, data['Medicine Name'])
38
+ if not matches:
39
+ return f"Không tìm thấy thuốc gần đúng với '{input_name}'. Vui lòng thử lại."
40
+
41
+ selected_medicine = matches[0]
42
+ idx = data[data['Medicine Name'] == selected_medicine].index[0]
43
+ sim_scores = list(enumerate(overall_similarity[idx]))
44
+ sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
45
+
46
+ recommendations = []
47
+ for i in sim_scores[1:top_n+1]:
48
+ med_info = data.iloc[i[0]]
49
+ recommendations.append({
50
+ "Medicine Name": med_info['Medicine Name'],
51
+ "Composition": med_info['Composition'],
52
+ "Uses": med_info['Uses'],
53
+ "Side Effects": med_info['Side_effects'],
54
+ "Manufacturer": med_info['Manufacturer'],
55
+ "Image URL": med_info['Image URL']
56
+ })
57
+ return recommendations
58
+
59
+ # Gradio interface function
60
+ def gradio_recommend(input_name):
61
+ results = recommend_medicine(input_name)
62
+ if isinstance(results, str): # Error message
63
+ return results
64
+
65
+ output = ""
66
+ for med in results:
67
+ output += f"### Tên thuốc: {med['Medicine Name'].capitalize()}\n"
68
+ output += f"**Thành phần:** {med['Composition']}\n"
69
+ output += f"**Công dụng:** {med['Uses']}\n"
70
+ output += f"**Tác dụng phụ:** {med['Side Effects']}\n"
71
+ output += f"**Nhà sản xuất:** {med['Manufacturer']}\n"
72
+ if med['Image URL']:
73
+ output += f"![Hình ảnh thuốc]({med['Image URL']})\n"
74
+ output += "\n"
75
+ return output
76
+
77
+ # Gradio app
78
+ interface = gr.Interface(
79
+ fn=gradio_recommend,
80
+ inputs="text",
81
+ outputs="markdown",
82
+ title="Gợi ý Thuốc Tương Tự",
83
+ description="Nhập tên thuốc để nhận danh sách các thuốc tương tự dựa trên thành phần và công dụng."
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ interface.launch()