rexoscare commited on
Commit
630d709
1 Parent(s): e95baf4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from llama_index.llms.anthropic import Anthropic
2
+ from llama_index.llms.mistralai import MistralAI
3
+ from llama_index.embeddings.mistralai import MistralAIEmbedding
4
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
5
+ from llama_index.core.settings import Settings
6
+ from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
7
+ import gradio as gr
8
+ from gradio_pdf import PDF
9
+ import os
10
+
11
+ choices = ['open-mistral-7b', 'claude-3-haiku']
12
+
13
+ def model_selection(choices):
14
+ if choices == "open-mistral-7b":
15
+ api_key = 'lJzlUC91kbvbMlOqCdAVorDdnmLEIU8b'
16
+ llm = MistralAI(api_key=api_key, model="open-mistral-7b")
17
+ embed_model = MistralAIEmbedding(model_name='mistral-embed', api_key=api_key)
18
+
19
+ else:
20
+ os.environ["ANTHROPIC_API_KEY"] = "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"
21
+ llm = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"], model = 'claude-3-haiku-20240307')
22
+ embed_model = HuggingFaceEmbedding(model_name='BAAI/bge-base-en-v1.5')
23
+
24
+ Settings.llm = llm
25
+ Settings.embed_model = embed_model
26
+
27
+ return choices
28
+
29
+ def qa(question: str, doc: str) -> str:
30
+ my_pdf = SimpleDirectoryReader(input_files=[doc]).load_data()
31
+ my_pdf_index = VectorStoreIndex.from_documents(my_pdf)
32
+ my_pdf_engine = my_pdf_index.as_query_engine()
33
+ response = my_pdf_engine.query(question)
34
+ return response
35
+
36
+ with gr.Blocks() as demo:
37
+ with gr.Row():
38
+ with gr.Column():
39
+ model_choice = gr.Radio(choices=choices, label = 'Choose a model')
40
+ #select_button = gr.Button("Select Model")
41
+ model_choice.change(model_selection, inputs=model_choice)
42
+ pdf_input = gr.File(label="Upload PDF")
43
+
44
+ with gr.Column():
45
+ #pdf_input = gr.File(label="Upload PDF")
46
+ question_input = gr.Textbox(label="Ask Question from PDF document")
47
+ qa_button = gr.Button("Get Answer")
48
+ answer_output = gr.Textbox(label="Answer")
49
+ qa_button.click(fn=qa, inputs=[question_input, pdf_input], outputs=answer_output)
50
+
51
+ demo.launch(debug = True)
52
+