Spaces:
Running
Running
File size: 5,357 Bytes
26ef710 707620f 26ef710 bd6783b 26ef710 bd6783b 26ef710 bd6783b 26ef710 7c1e061 26ef710 bd6783b 26ef710 bd6783b 26ef710 707620f 26ef710 bd6783b 26ef710 bd6783b 26ef710 bd6783b 26ef710 7c1e061 26ef710 707620f 26ef710 7c1e061 26ef710 |
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 |
import gradio as gr
import os
from dotenv import load_dotenv
from pathlib import Path
from functions import (
reset_open_ai,
fetch_collections,
select_collection,
reset_collection,
handle_submit,
)
from models import (
Icons,
Message,
Messages,
)
load_dotenv()
css_style = Path('./style.css').read_text()
with gr.Blocks(css=css_style) as demo:
zot = gr.State(None)
zot_collections = gr.State([])
data = gr.State([])
messages = gr.State(Messages([
Message(
Icons.INFO, "Please provide all the required OpenAI and Zotero information in the left panel."),
]))
with gr.Row():
gr.Markdown("""
<h2>
<img src='file/assets/zotero-logo.png' alt="Zotero Logo" style="width: 3.2rem; display: inline; margin-right: 10px;" />
Zotero Q&A
</h2>
Fan Li ([@FanLi_RnD](https://twitter.com/FanLi_RnD)) - https://apex974.com/articles/literature-reviews-with-paper-qa-and-zotero
This tool allows you to ask questions based on your Zotero library. It was built upon [Paper QA](https://github.com/whitead/paper-qa), [LangChain AI](https://github.com/hwchase17/langchain) and [pyZotero](https://github.com/urschrei/pyzotero).
""")
with gr.Row():
with gr.Column(scale=1):
openai_api_key = gr.Textbox(
label="OpenAI API Key", type="password", value=os.getenv('OPENAI_API_KEY'))
gr.HTML()
zot_api_key = gr.Textbox(
label="Zotero API Key", type="password", value=os.getenv('ZOTERO_API_KEY'))
zot_library_type = gr.Radio(choices=[
"User", "Group"], label="Zotero Library Type", value="User", elem_id="zotero-library-type")
zot_library_id = gr.Textbox(
label="Zotero User/Group ID", value=os.getenv('ZOTERO_LIBRARY_ID'))
zot_citation_style = gr.Textbox(
label="Zotero Citation Style",
value='nature',
)
zot_selected_col = gr.Radio(
[], label="Zotero Collection", elem_id="zotero-collection", visible=False)
zot_msg = gr.HTML("""
<div style="padding: 1rem; background-color: #fffbe7; font-size: 0.8rem;">
<ul style="margin-bottom: 0;">
<li>Click <a href="https://www.zotero.org/settings/keys/new" target="_blank">here</a> to create Zotero API key.</li>
<li>To access your own library, select "User".</li>
<li>To access a shared group, select "Group".</li>
<li>Personal User ID can be found <a href="https://www.zotero.org/settings/keys" target="_blank">here</a>.</li>
<li style="margin-bottom: 0;">Group ID is part of the group URL (e.g. 4952526).</li>
</ul>
</div>
""", visible=True)
zot_fetch_col_btn = gr.Button('Fetch Collections')
gr.Error("Some Error Message")
with gr.Column(scale=3):
question = gr.Textbox(
placeholder="You have to select a Zotero collection to proceed", label="Question", interactive=False)
gr.HTML()
with gr.Box():
with gr.Accordion("Messages"):
msg_board = gr.HTML(messages.value)
answer = gr.HTML(None, elem_id="answer")
openai_api_key.change(reset_open_ai, inputs=[openai_api_key], outputs=[
answer], show_progress=False)
zot_api_key.change(
reset_collection,
inputs=[messages],
outputs=[zot_selected_col, zot_fetch_col_btn,
question, answer, messages, msg_board, zot_selected_col],
show_progress=False,
)
zot_library_type.change(
reset_collection,
inputs=[messages],
outputs=[zot_selected_col, zot_fetch_col_btn,
question, answer, messages, msg_board, zot_selected_col],
show_progress=False,
)
zot_library_id.change(
reset_collection,
inputs=[messages],
outputs=[zot_selected_col, zot_fetch_col_btn,
question, answer, messages, msg_board, zot_selected_col],
show_progress=False
)
zot_citation_style.change(
reset_collection,
inputs=[messages],
outputs=[zot_selected_col, zot_fetch_col_btn,
question, answer, messages, msg_board, zot_selected_col],
show_progress=False
)
zot_fetch_col_btn.click(
fn=fetch_collections,
inputs=[openai_api_key, zot_library_id,
zot_library_type, zot_api_key, messages],
outputs=[zot, zot_collections, zot_selected_col,
zot_fetch_col_btn, zot_msg, messages, msg_board],
show_progress=False,
)
zot_selected_col.change(
fn=select_collection,
inputs=[zot_selected_col, messages],
outputs=[question, messages, msg_board, answer],
show_progress=False
)
question.submit(
fn=handle_submit,
inputs=[zot, zot_selected_col, zot_collections, zot_citation_style, question, messages],
outputs=[messages, msg_board, answer],
show_progress=False
)
demo.queue(concurrency_count=10, api_open=False)
demo.launch()
|