Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils.upload_file import UploadFile
|
| 3 |
+
from utils.chatbot import ChatBot
|
| 4 |
+
from utils.ui_settings import UISettings
|
| 5 |
+
from utils.load_config import LoadConfig
|
| 6 |
+
|
| 7 |
+
APPCFG = LoadConfig()
|
| 8 |
+
# # Prepare the LLm and Tokenizer
|
| 9 |
+
# tokenizer = AutoTokenizer.from_pretrained(
|
| 10 |
+
# APPCFG.llm_engine, token=APPCFG.gemma_token, device=APPCFG.device)
|
| 11 |
+
# model = model = AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path="google/gemma-7b-it",
|
| 12 |
+
# token=APPCFG.gemma_token,
|
| 13 |
+
# torch_dtype=torch.float16,
|
| 14 |
+
# device_map=APPCFG.device
|
| 15 |
+
# )
|
| 16 |
+
# app_pipeline = pipeline(
|
| 17 |
+
# "text-generation",
|
| 18 |
+
# model=model,
|
| 19 |
+
# tokenizer=tokenizer
|
| 20 |
+
# )
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
with gr.Tabs():
|
| 23 |
+
with gr.TabItem("Med-App"):
|
| 24 |
+
##############
|
| 25 |
+
# First ROW:
|
| 26 |
+
##############
|
| 27 |
+
with gr.Row() as row_one:
|
| 28 |
+
with gr.Column(visible=False) as reference_bar:
|
| 29 |
+
ref_output = gr.Markdown()
|
| 30 |
+
|
| 31 |
+
with gr.Column() as chatbot_output:
|
| 32 |
+
chatbot = gr.Chatbot(
|
| 33 |
+
[],
|
| 34 |
+
elem_id="chatbot",
|
| 35 |
+
bubble_full_width=False,
|
| 36 |
+
height=500,
|
| 37 |
+
avatar_images=(
|
| 38 |
+
("images/test.png"), "images/Gemma-logo.png"),
|
| 39 |
+
# render=False
|
| 40 |
+
)
|
| 41 |
+
# **Adding like/dislike icons
|
| 42 |
+
chatbot.like(UISettings.feedback, None, None)
|
| 43 |
+
##############
|
| 44 |
+
# SECOND ROW:
|
| 45 |
+
##############
|
| 46 |
+
with gr.Row():
|
| 47 |
+
input_txt = gr.Textbox(
|
| 48 |
+
lines=4,
|
| 49 |
+
scale=8,
|
| 50 |
+
placeholder="Enter text and press enter, or upload PDF files",
|
| 51 |
+
container=False,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
##############
|
| 55 |
+
# Third ROW:
|
| 56 |
+
##############
|
| 57 |
+
with gr.Row() as row_two:
|
| 58 |
+
text_submit_btn = gr.Button(value="Submit text")
|
| 59 |
+
sidebar_state = gr.State(False)
|
| 60 |
+
btn_toggle_sidebar = gr.Button(
|
| 61 |
+
value="References")
|
| 62 |
+
btn_toggle_sidebar.click(UISettings.toggle_sidebar, [sidebar_state], [
|
| 63 |
+
reference_bar, sidebar_state])
|
| 64 |
+
upload_btn = gr.UploadButton(
|
| 65 |
+
"📁 Upload PDF or doc files", file_types=[
|
| 66 |
+
'.pdf',
|
| 67 |
+
'.doc'
|
| 68 |
+
],
|
| 69 |
+
file_count="multiple")
|
| 70 |
+
clear_button = gr.ClearButton([input_txt, chatbot])
|
| 71 |
+
rag_with_dropdown = gr.Dropdown(
|
| 72 |
+
label="RAG with", choices=["Preprocessed doc", "Upload doc: Process for RAG"], value="Preprocessed doc")
|
| 73 |
+
##############
|
| 74 |
+
# Fourth ROW:
|
| 75 |
+
##############
|
| 76 |
+
with gr.Row() as row_four:
|
| 77 |
+
temperature_bar = gr.Slider(minimum=0.1, maximum=1, value=0.1, step=0.1,
|
| 78 |
+
label="Temperature", info="Increasing the temperature will make the model answer more creatively.")
|
| 79 |
+
top_k = gr.Slider(minimum=0.0,
|
| 80 |
+
maximum=100.0,
|
| 81 |
+
step=1,
|
| 82 |
+
label="top_k",
|
| 83 |
+
value=50,
|
| 84 |
+
info="A lower value (e.g. 10) will result in more conservative answers.")
|
| 85 |
+
top_p = gr.Slider(minimum=0.0,
|
| 86 |
+
maximum=1.0,
|
| 87 |
+
step=0.01,
|
| 88 |
+
label="top_p",
|
| 89 |
+
value=0.95,
|
| 90 |
+
info=" Works together with top-k. lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.0)")
|
| 91 |
+
|
| 92 |
+
##############
|
| 93 |
+
# Process:
|
| 94 |
+
##############
|
| 95 |
+
file_msg = upload_btn.upload(fn=UploadFile.process_uploaded_files, inputs=[
|
| 96 |
+
upload_btn, chatbot, rag_with_dropdown], outputs=[input_txt, chatbot], queue=False)
|
| 97 |
+
|
| 98 |
+
txt_msg = input_txt.submit(fn=ChatBot.respond,
|
| 99 |
+
inputs=[chatbot, input_txt,
|
| 100 |
+
rag_with_dropdown, temperature_bar, top_k, top_p],
|
| 101 |
+
outputs=[input_txt,
|
| 102 |
+
chatbot, ref_output],
|
| 103 |
+
queue=False).then(lambda: gr.Textbox(interactive=True),
|
| 104 |
+
None, [input_txt], queue=False)
|
| 105 |
+
|
| 106 |
+
txt_msg = text_submit_btn.click(fn=ChatBot.respond,
|
| 107 |
+
inputs=[chatbot, input_txt,
|
| 108 |
+
rag_with_dropdown, temperature_bar, top_k, top_p],
|
| 109 |
+
outputs=[input_txt,
|
| 110 |
+
chatbot, ref_output],
|
| 111 |
+
queue=False).then(lambda: gr.Textbox(interactive=True),
|
| 112 |
+
None, [input_txt], queue=False)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
demo.launch()
|