Bart Cant commited on
Commit
6671112
·
1 Parent(s): 9168834

initial comit

Browse files
Files changed (2) hide show
  1. app.py +146 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+
4
+ # Get Hugging Face API Key
5
+ huggingface_token = os.environ.get('HUGGINGFACE_API_KEY')
6
+
7
+ # PubMed Fetcher
8
+ from pymed import PubMed
9
+ from typing import List
10
+ from haystack import component
11
+ from haystack import Document
12
+
13
+
14
+
15
+ def predict( message, history):
16
+
17
+ pubmed = PubMed(tool="Tydei", email="info@tydei.io")
18
+
19
+ def documentize(article):
20
+ return Document(content=article.abstract, meta={'title': article.title, 'keywords': article.keywords, 'journal': article.journal})
21
+
22
+ @component
23
+ class PubMedFetcher():
24
+ @component.output_types(articles=List[Document])
25
+ def run(self, queries: list[str]):
26
+ cleaned_queries = queries[0].strip().split('\n')
27
+ articles = []
28
+ try:
29
+ for query in cleaned_queries:
30
+ response = pubmed.query(query, max_results=3)
31
+ documents = [documentize(article) for article in response]
32
+ articles.extend(documents)
33
+ except Exception as e:
34
+ print(e)
35
+ print(f"Couldn't fetch articles for queries: {queries}")
36
+ results = {'articles': articles}
37
+ print("-----------------------------")
38
+ print ("results : ", results)
39
+ # Initialize an empty list to store all keywords
40
+ all_keywords = []
41
+
42
+ # Iterate over each Document in the articles list
43
+ for document in articles:
44
+ # Extract the keywords from the meta attribute of each Document
45
+ keywords = document.meta.get('keywords', [])
46
+ # Extend the all_keywords list with the extracted keywords
47
+ all_keywords.extend(keywords)
48
+
49
+ # Add the extracted keywords to the results dictionary
50
+ results['keywords'] = all_keywords
51
+ # Print all extracted keywords
52
+ print("Extracted Keywords:", all_keywords)
53
+
54
+ return results
55
+
56
+ # Pipeline Setup
57
+ from haystack.components.generators import HuggingFaceTGIGenerator
58
+ from haystack import Pipeline
59
+ from haystack.components.builders.prompt_builder import PromptBuilder
60
+
61
+ keyword_llm = HuggingFaceTGIGenerator("mistralai/Mixtral-8x7B-Instruct-v0.1", token=huggingface_token)
62
+ keyword_llm.warm_up()
63
+
64
+ llm = HuggingFaceTGIGenerator("mistralai/Mixtral-8x7B-Instruct-v0.1", token=huggingface_token)
65
+ llm.warm_up()
66
+
67
+ from haystack import Pipeline
68
+ from haystack.components.builders.prompt_builder import PromptBuilder
69
+
70
+ keyword_prompt_template = """
71
+ Your task is to convert the follwing question into 5 keywords that can be used to find relevant medical research papers on PubMed.
72
+
73
+ ---
74
+ question: {{ question }}
75
+ keywords:
76
+ """
77
+
78
+ prompt_template = """
79
+ Answer the question truthfully based on the given documents.
80
+ Only use data from the given documents to answer the question.
81
+
82
+ q: {{ question }}
83
+
84
+ Articles:
85
+ {% for article in articles %}
86
+ {{article.content}}
87
+ keywords: {{article.meta['keywords']}}
88
+ title: {{article.meta['title']}}
89
+ journal: {{article.journal}}
90
+ {% endfor %}
91
+
92
+ Only provide an answer based on the information give. Do not make anything up.
93
+
94
+ At the end of your answer create a Source List with the
95
+ name of Article title and Journal. Retrieve your information from the article meta data
96
+
97
+ DO not repeat yourself in the answer and ONLY use information from the given articles. DO NOT USE Your Own Knowledge base or you will be punished
98
+ """
99
+
100
+ keyword_prompt_builder = PromptBuilder(template=keyword_prompt_template)
101
+ prompt_builder = PromptBuilder(template=prompt_template)
102
+ fetcher = PubMedFetcher()
103
+
104
+ pipe = Pipeline()
105
+ pipe.add_component("keyword_prompt_builder", keyword_prompt_builder)
106
+ pipe.add_component("keyword_llm", keyword_llm)
107
+ pipe.add_component("pubmed_fetcher", fetcher)
108
+ pipe.add_component("prompt_builder", prompt_builder)
109
+ pipe.add_component("llm", llm)
110
+
111
+ pipe.connect("keyword_prompt_builder.prompt", "keyword_llm.prompt")
112
+ pipe.connect("keyword_llm.replies", "pubmed_fetcher.queries")
113
+ pipe.connect("pubmed_fetcher.articles", "prompt_builder.articles")
114
+ pipe.connect("prompt_builder.prompt", "llm.prompt")
115
+
116
+
117
+ # Function to Ask Questions
118
+ def ask(question):
119
+ result = pipe.run(data={"keyword_prompt_builder":{"question":question},
120
+ "prompt_builder":{"question": question},
121
+ "llm":{"generation_kwargs": {"max_new_tokens": 1500}}})
122
+
123
+ print("-----------------------------")
124
+ print("Question is: ", question)
125
+ print("-----------------------------")
126
+ print(result)
127
+ output = result['llm']['replies'][0]
128
+ keywords = result['pubmed_fetcher']['keywords']
129
+ return keywords, output
130
+
131
+
132
+ keywords , output= ask(message)
133
+
134
+ return keywords, output
135
+
136
+ # Launch Gradio Interface
137
+
138
+ interface = gr.Interface(
139
+ fn=predict,
140
+ inputs= [gr.Textbox(label="Research Pubmed...", lines=2, placeholder="Type Question here..."),
141
+ gr.Dropdown(["ortho", "anthroscopy", "osteotomu", "fracture repair"], value=["spinal fusion", "bone grafting"], multiselect=True, label="Keyword Entry")],
142
+ outputs=[gr.Textbox(label="Keywords used in researching articles", lines=5),
143
+ gr.Textbox(label="Pubmed Insights", lines=20)]
144
+ )
145
+
146
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ pymongo==4.6.1
2
+ langchain-core==0.1.9
3
+ langchain==0.1.0
4
+ langchain-community==0.0.11
5
+ langchain-openai==0.0.2
6
+ openai==1.6.1
7
+ tiktoken==0.5.2