DrishtiSharma commited on
Commit
02ca4c4
1 Parent(s): d0f06da

Create app.py

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