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