NAB1108 commited on
Commit
2362996
1 Parent(s): 71a590b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -29,17 +29,24 @@ from langchain.indexes import VectorstoreIndexCreator
29
  import tempfile
30
  from langchain.document_loaders import DirectoryLoader
31
 
32
- os.environ["OPENAI_API_KEY"] = os.environ['OpenApi_Key']
33
  query1=" "
34
  limit = 0
35
  def loading_pdf():
36
  return "Loading..."
37
 
38
-
 
 
 
 
 
 
 
39
 
40
  def pdf_changes(pdf_doc, prompt):
41
 
42
- loader = DirectoryLoader("/tmp/gradio")
43
  #loader = OnlinePDFLoader(pdf_doc.name)
44
  #loader = PyPDFLoader(pdf_doc.name)
45
  documents = loader.load()
@@ -63,7 +70,7 @@ def pdf_changes(pdf_doc, prompt):
63
  )
64
 
65
 
66
- system_template=prompt+"""You are a helpful chatbot used by the user to chat with pdf documents. Only answer the questions by using information provided in the context provided to you. If there is no relavant context, tell 'Hmm, I'm not sure'.
67
  ----------------
68
  {summaries}"""
69
 
@@ -77,7 +84,7 @@ def pdf_changes(pdf_doc, prompt):
77
  from langchain.chains import RetrievalQAWithSourcesChain
78
  global query1
79
  chain_type_kwargs = {"prompt": prompt2}
80
- llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, max_tokens=512) # Modify model_name if you have access to GPT-4
81
  global chain
82
  chain = RetrievalQAWithSourcesChain.from_chain_type(
83
  llm=llm,
@@ -100,7 +107,7 @@ def bot(history):
100
  def infer(question):
101
  global query1
102
  global limit
103
- openai.api_key = os.environ['OpenApi_Key']
104
  prompt_text = question
105
  if prompt_text:
106
  query1 = query1 + "\nUser: " + prompt_text + "\nBot: "
@@ -134,7 +141,7 @@ with gr.Blocks(css=css,theme = gr.themes.Soft()) as demo:
134
  with gr.Row():
135
  with gr.Column(scale=1):
136
  #gr.File(file_count="multiple")
137
- pdf_doc = gr.File(label="Load a pdf", file_count="multiple")
138
  prompt = gr.Textbox(label="Behaviour Prompt (optional)", placeholder="Reply to all questions as a rap / Reply to all questions in Hindi etc. ")
139
  #repo_id = gr.Dropdown(label="LLM", choices=["google/flan-ul2", "OpenAssistant/oasst-sft-1-pythia-12b", "bigscience/bloomz"], value="google/flan-ul2")
140
  with gr.Row():
@@ -145,8 +152,6 @@ with gr.Blocks(css=css,theme = gr.themes.Soft()) as demo:
145
  with gr.Row():
146
  question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ",scale=6,show_label=False)
147
  submit_btn = gr.Button("Send",scale=1)
148
- #load_pdf.click(loading_pdf, None, langchain_status, queue=False)
149
- #repo_id.change(pdf_changes, inputs=[pdf_doc], outputs=[langchain_status], queue=False)
150
  load_pdf.click(pdf_changes, inputs=[pdf_doc,prompt], outputs=[langchain_status], queue=False)
151
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
152
  bot, chatbot, chatbot
 
29
  import tempfile
30
  from langchain.document_loaders import DirectoryLoader
31
 
32
+ os.environ["OPENAI_API_KEY"] = "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"
33
  query1=" "
34
  limit = 0
35
  def loading_pdf():
36
  return "Loading..."
37
 
38
+ def remove_last_slash_and_characters(input_string):
39
+ last_slash_index = input_string.rfind('/')
40
+ if last_slash_index != -1:
41
+ result_string = input_string[:last_slash_index]
42
+ else:
43
+ result_string = input_string
44
+
45
+ return result_string
46
 
47
  def pdf_changes(pdf_doc, prompt):
48
 
49
+ loader = DirectoryLoader(remove_last_slash_and_characters(pdf_doc.name))
50
  #loader = OnlinePDFLoader(pdf_doc.name)
51
  #loader = PyPDFLoader(pdf_doc.name)
52
  documents = loader.load()
 
70
  )
71
 
72
 
73
+ system_template="""You are a helpful chatbot used by the user to chat with pdf documents. Only answer the questions by using information provided in the context provided to you. If there is no relavant context, tell 'Hmm, I'm not sure'."""+prompt+"""
74
  ----------------
75
  {summaries}"""
76
 
 
84
  from langchain.chains import RetrievalQAWithSourcesChain
85
  global query1
86
  chain_type_kwargs = {"prompt": prompt2}
87
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, max_tokens=512)
88
  global chain
89
  chain = RetrievalQAWithSourcesChain.from_chain_type(
90
  llm=llm,
 
107
  def infer(question):
108
  global query1
109
  global limit
110
+ openai.api_key = "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"
111
  prompt_text = question
112
  if prompt_text:
113
  query1 = query1 + "\nUser: " + prompt_text + "\nBot: "
 
141
  with gr.Row():
142
  with gr.Column(scale=1):
143
  #gr.File(file_count="multiple")
144
+ pdf_doc = gr.File(label="Load a pdf", file_count="single")
145
  prompt = gr.Textbox(label="Behaviour Prompt (optional)", placeholder="Reply to all questions as a rap / Reply to all questions in Hindi etc. ")
146
  #repo_id = gr.Dropdown(label="LLM", choices=["google/flan-ul2", "OpenAssistant/oasst-sft-1-pythia-12b", "bigscience/bloomz"], value="google/flan-ul2")
147
  with gr.Row():
 
152
  with gr.Row():
153
  question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ",scale=6,show_label=False)
154
  submit_btn = gr.Button("Send",scale=1)
 
 
155
  load_pdf.click(pdf_changes, inputs=[pdf_doc,prompt], outputs=[langchain_status], queue=False)
156
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
157
  bot, chatbot, chatbot