Download app.py from bartcant/pubmed: direct link, hf CLI and curl.
- Browser
- Download file 5.51 kB
-
https://huggingface.co/spaces/bartcant/pubmed/resolve/main/app.py
- Command line
-
hf download hf://spaces/bartcant/pubmed/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bartcant/pubmed/resolve/main/app.py
5.51 kB
| 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}) | |
| class PubMedFetcher(): | |
| 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() |