soham07ml commited on
Commit
b1e6aa3
1 Parent(s): 7e45312

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +128 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pymed import PubMed
2
+ from typing import List
3
+ from haystack import component
4
+ from haystack import Document
5
+ from haystack.components.generators import HuggingFaceTGIGenerator #in between this we will use mixtral 7 xb model from text generation interface
6
+ from dotenv import load_dotenv
7
+ import os
8
+ from haystack import Pipeline #other names for chains
9
+ from haystack. components.builders.prompt_builder import PromptBuilder
10
+ import gradio as gr
11
+ import time
12
+
13
+ load_dotenv
14
+
15
+ # Attempt to get the 'HUGGINGFACE_API_KEY' from the environment variables
16
+ api_key = os.getenv('HUGGINGFACE_API_KEY')
17
+
18
+ # Check if the 'HUGGINGFACE_API_KEY' is set
19
+ if api_key is not None:
20
+ # Set the 'HUGGINGFACE_API_KEY' in the environment variables
21
+ os.environ['HUGGINGFACE_API_KEY'] = api_key
22
+ else:
23
+ # Handle the case when 'HUGGINGFACE_API_KEY' is not set
24
+ print("HUGGINGFACE_API_KEY is not set. Please set it in your environment variables.")
25
+
26
+ pubmed = PubMed(tool="Haystack2.0Prototype", email="dummyemail@gmail.com")
27
+
28
+ def documentize(article):
29
+ return Document(content=article.abstract, meta={'title': article.title, 'keywords': article.keywords})
30
+
31
+ @component
32
+ class PubMedFetcher():
33
+
34
+ @component.output_types(articles=List[Document])
35
+ def run(self, queries: list[str]):
36
+ cleaned_queries = queries[0].strip().split('\n')
37
+
38
+ articles = []
39
+ try:
40
+ for query in cleaned_queries:
41
+ response = pubmed.query(query, max_results = 1)
42
+ documents = [documentize(article) for article in response]
43
+ articles.extend(documents)
44
+ except Exception as e:
45
+ print(e)
46
+ print(f"Couldn't fetch articles for queries: {queries}" )
47
+ results = {'articles': articles}
48
+ return results
49
+
50
+ keyword_llm = HuggingFaceTGIGenerator("mistralai/Mixtral-8x7B-Instruct-v0.1")
51
+ keyword_llm.warm_up()
52
+
53
+ llm = HuggingFaceTGIGenerator("mistralai/Mixtral-8x7B-Instruct-v0.1")
54
+ llm.warm_up()
55
+
56
+
57
+ keyword_prompt_template = """
58
+ Your task is to convert the following question into 3 keywords that can be used to find relevant medical research papers on PubMed.
59
+ Here is an examples:
60
+ question: "What are the latest treatments for major depressive disorder?"
61
+ keywords:
62
+ Antidepressive Agents
63
+ Depressive Disorder, Major
64
+ Treatment-Resistant depression
65
+ ---
66
+ question: {{ question }}
67
+ keywords:
68
+ """
69
+
70
+ prompt_template = """
71
+ Answer the question truthfully based on the given documents.
72
+ If the documents don't contain an answer, use your existing knowledge base.
73
+
74
+
75
+ q: {{ question }}
76
+ Articles:
77
+ {% for article in articles %}
78
+ {{article.content}}
79
+ keywords: {{article.meta['keywords']}}
80
+ title: {{article.meta['title']}}
81
+ {% endfor %}
82
+
83
+ """
84
+
85
+ keyword_prompt_builder = PromptBuilder(template=keyword_prompt_template)
86
+
87
+ prompt_builder = PromptBuilder(template=prompt_template)
88
+ fetcher = PubMedFetcher()
89
+
90
+ pipe = Pipeline()
91
+
92
+ pipe.add_component("keyword_prompt_builder", keyword_prompt_builder)
93
+ pipe.add_component("keyword_llm", keyword_llm)
94
+ pipe.add_component("pubmed_fetcher", fetcher)
95
+ pipe.add_component("prompt_builder", prompt_builder)
96
+ pipe.add_component("llm", llm)
97
+
98
+ pipe.connect("keyword_prompt_builder.prompt", "keyword_llm.prompt")
99
+ pipe.connect("keyword_llm.replies", "pubmed_fetcher.queries")
100
+
101
+ pipe.connect("pubmed_fetcher.articles", "prompt_builder.articles")
102
+ pipe.connect("prompt_builder.prompt", "llm.prompt")
103
+
104
+ def ask(question):
105
+ output = pipe.run(data={"keyword_prompt_builder":{"question":question},
106
+ "prompt_builder":{"question": question},
107
+ "llm":{"generation_kwargs": {"max_new_tokens": 500}}})
108
+ print(question)
109
+ print(output['llm']['replies'][0])
110
+ return output['llm']['replies'][0]
111
+
112
+ # result = ask("How are mRNA vaccines being used for cancer treatment?")
113
+
114
+ # print(result)
115
+
116
+ iface = gr.Interface(fn=ask, inputs=gr.Textbox(
117
+ value="How are mRNA vaccines being used for cancer treatment?"),
118
+ outputs="markdown",
119
+ title="LLM Augmented Q&A over PubMed Search Engine",
120
+ description="Ask a question about BioMedical and get an answer from a friendly AI assistant.",
121
+ examples=[["How are mRNA vaccines being used for cancer treatment?"],
122
+ ["Suggest me some Case Studies related to Pneumonia."],
123
+ ["Tell me about HIV AIDS."],["Suggest some case studies related to Auto Immune Disorders."],
124
+ ["How to treat a COVID infected Patient?"]],
125
+ theme=gr.themes.Soft(),
126
+ allow_flagging="never",)
127
+
128
+ iface.launch(debug=True,share=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ haystack-ai
2
+ pymed
3
+ gradio
4
+ python-dotenv
5
+ transformers