aakash0017 commited on
Commit
7627550
·
1 Parent(s): b286610

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. conv_app.py +34 -2
  2. conversation.py +152 -13
conv_app.py CHANGED
@@ -1,9 +1,41 @@
1
  import gradio as gr
2
- from conversation import make_conversation, auth_function
3
  import random
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  with gr.Blocks(css="style.css") as demo:
6
- gr.Markdown("##DR. VAI")
 
 
7
  gr.ChatInterface(make_conversation).queue()
8
 
9
  demo.launch()
 
1
  import gradio as gr
2
+ from conversation import make_conversation
3
  import random
4
+ import time
5
+
6
+ global USERNAME
7
+ global PASSWORD
8
+ global INPUT
9
+ global OUTPUT
10
+ global SOURCE
11
+ global DOCS
12
+
13
+ # def auth_function(username, password):
14
+ # USERNAME = username
15
+ # user_name = username
16
+ # return username == password
17
+
18
+
19
+ # def make_conversation(message, history):
20
+ # INPUT = message
21
+ # text_, source, docs = run(message)
22
+ # OUTPUT = text_
23
+ # SOURCE = source
24
+ # DOCS = docs
25
+
26
+ # # print("INPUT: ", INPUT)
27
+ # # print("OUTPUT: ", OUTPUT)
28
+ # # print("SOURCE: ", SOURCE)
29
+ # # print("DOCS: ", DOCS)
30
+
31
+ # for i in range(len(text_)):
32
+ # time.sleep(0.001)
33
+ # yield text_[: i+1]
34
 
35
  with gr.Blocks(css="style.css") as demo:
36
+ gr.Markdown("""
37
+ # Dr. V AI
38
+ """)
39
  gr.ChatInterface(make_conversation).queue()
40
 
41
  demo.launch()
conversation.py CHANGED
@@ -1,3 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from langchain.document_loaders import TextLoader
2
  import pinecone
3
  from langchain.vectorstores import Pinecone
@@ -14,6 +134,8 @@ from langchain.prompts import MessagesPlaceholder
14
  import gradio as gr
15
  import time
16
  from db_func import insert_one
 
 
17
 
18
  def get_bert_embeddings(sentence):
19
  embeddings = []
@@ -24,8 +146,8 @@ def get_bert_embeddings(sentence):
24
  return embedding
25
 
26
  model_name = "BAAI/bge-base-en-v1.5"
27
- model = AutoModel.from_pretrained(model_name)
28
- tokenizer = AutoTokenizer.from_pretrained(model_name)
29
  prompt_file = open("prompts/version_2.txt", "r").read()
30
 
31
  pinecone.init(
@@ -43,21 +165,38 @@ tool = create_retriever_tool(
43
  "Searches and returns documents regarding the ophtal-knowledge-base.",
44
  )
45
  tools = [tool]
46
- system_message = SystemMessage(content=prompt_file)
47
  memory_key='history'
48
 
49
  llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4", temperature=0.2)
 
50
  prompt = OpenAIFunctionsAgent.create_prompt(
51
  system_message=system_message,
52
  extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],
53
  )
54
 
55
- agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=False, prompt=prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  user_name = None
57
 
58
  def run(input_):
59
  output = agent_executor({"input": input_})
60
  output_text = output["output"]
 
61
  source_text = ""
62
  doc_text = ""
63
 
@@ -87,19 +226,19 @@ def run(input_):
87
  "documents": doc_text
88
  }
89
 
90
- insert_one(doc_to_insert)
91
 
92
  return output_text
93
 
94
- def make_conversation(message, history):
95
- text_ = run(message)
96
- for i in range(len(text_)):
97
- time.sleep(0.001)
98
- yield text_[: i+1]
99
 
100
- def auth_function(username, password):
101
- user_name = username
102
- return username == password
103
 
104
 
105
 
 
1
+ # from langchain.document_loaders import TextLoader
2
+ # import pinecone
3
+ # from langchain.vectorstores import Pinecone
4
+ # import os
5
+ # from transformers import AutoTokenizer, AutoModel
6
+ # from langchain.agents.agent_toolkits import create_conversational_retrieval_agent
7
+ # from langchain.agents.agent_toolkits import create_retriever_tool
8
+ # from langchain.chat_models import ChatOpenAI
9
+ # import torch
10
+ # from langchain.agents.openai_functions_agent.agent_token_buffer_memory import (AgentTokenBufferMemory)
11
+ # from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
12
+ # from langchain.schema.messages import SystemMessage
13
+ # from langchain.prompts import MessagesPlaceholder
14
+ # import gradio as gr
15
+ # import time
16
+ # from db_func import insert_one
17
+ # from global_variable_module import gobal_input, global_output
18
+ # import random
19
+
20
+
21
+ # def get_bert_embeddings(sentence):
22
+ # embeddings = []
23
+ # input_ids = tokenizer.encode(sentence, return_tensors="pt")
24
+ # with torch.no_grad():
25
+ # output = model(input_ids)
26
+ # embedding = output.last_hidden_state[:,0,:].numpy().tolist()
27
+ # return embedding
28
+
29
+ # model_name = "BAAI/bge-base-en-v1.5"
30
+ # model = AutoModel.from_pretrained("/Users/aakashbhatnagar/Documents/masters/ophthal_llm/models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
31
+ # tokenizer = AutoTokenizer.from_pretrained("/Users/aakashbhatnagar/Documents/masters/ophthal_llm/models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
32
+ # prompt_file = open("prompts/version_2.txt", "r").read()
33
+
34
+ # pinecone.init(
35
+ # api_key=os.getenv("PINECONE_API_KEY"), # find at app.pinecone.io
36
+ # environment=os.getenv("PINECONE_ENV"), # next to api key in console
37
+ # )
38
+
39
+ # index_name = "ophtal-knowledge-base"
40
+ # index = pinecone.Index(index_name)
41
+ # vectorstore = Pinecone(index, get_bert_embeddings, "text")
42
+ # retriever = vectorstore.as_retriever()
43
+ # tool = create_retriever_tool(
44
+ # retriever,
45
+ # "search_ophtal-knowledge-base",
46
+ # "Searches and returns documents regarding the ophtal-knowledge-base.",
47
+ # )
48
+ # tools = [tool]
49
+ # system_message = SystemMessage(content=prompt_file)
50
+ # memory_key='history'
51
+
52
+ # llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4", temperature=0.2)
53
+ # prompt = OpenAIFunctionsAgent.create_prompt(
54
+ # system_message=system_message,
55
+ # extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],
56
+ # )
57
+
58
+ # agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=False, prompt=prompt)
59
+ # user_name = None
60
+
61
+ # def run(input_):
62
+ # output = agent_executor({"input": input_})
63
+ # output_text = output["output"]
64
+ # source_text = ""
65
+ # doc_text = ""
66
+ # global_input = input_
67
+ # global_output = output_text
68
+
69
+
70
+ # if len(output["intermediate_steps"])>0:
71
+ # documents = output["intermediate_steps"][0][1]
72
+ # sources = []
73
+ # docs = []
74
+
75
+ # for doc in documents:
76
+ # if doc.metadata["source"] not in sources:
77
+ # sources.append(doc.metadata["source"])
78
+ # docs.append(doc.page_content)
79
+
80
+ # for i in range(len(sources)):
81
+ # temp = sources[i].replace('.pdf', '').replace('.txt', '').replace("AAO", "").replace("2022-2023", "").replace("data/book", "").replace("text", "").replace(" ", " ")
82
+ # source_text += f"{i+1}. {temp}\n"
83
+ # doc_text += f"{i+1}. {docs[i]}\n"
84
+
85
+ # # output_text = f"{output_text} \n\nSources: \n{source_text}\n\nDocuments: \n{doc_text}"
86
+ # # output_text = f"{output_text}"
87
+
88
+ # doc_to_insert = {
89
+ # "user": user_name,
90
+ # "input": input_,
91
+ # "output": output_text,
92
+ # "source": source_text,
93
+ # "documents": doc_text
94
+ # }
95
+
96
+ # insert_one(doc_to_insert)
97
+
98
+ # return output_text
99
+
100
+ # def make_conversation(message, history):
101
+ # text_ = run(message)
102
+ # for i in range(len(text_)):
103
+ # time.sleep(0.001)
104
+ # yield text_[: i+1]
105
+
106
+ # def auth_function(username, password):
107
+ # user_name = username
108
+ # return username == password
109
+
110
+ # def random_response(message, accuracy, history):
111
+ # print(type(message))
112
+ # print(message)
113
+ # print(accuracy)
114
+ # out = random.choice(["Yes", "No"])
115
+ # gobal_input = out
116
+ # # open a txt file
117
+ # with open("function hit", "a+") as f:
118
+ # f.write(message)
119
+ # return out
120
+
121
  from langchain.document_loaders import TextLoader
122
  import pinecone
123
  from langchain.vectorstores import Pinecone
 
134
  import gradio as gr
135
  import time
136
  from db_func import insert_one
137
+ from langchain.agents import AgentExecutor
138
+
139
 
140
  def get_bert_embeddings(sentence):
141
  embeddings = []
 
146
  return embedding
147
 
148
  model_name = "BAAI/bge-base-en-v1.5"
149
+ model = AutoModel.from_pretrained("models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
150
+ tokenizer = AutoTokenizer.from_pretrained("models/models--BAAI--bge-base-en-v1.5/snapshots/617ca489d9e86b49b8167676d8220688b99db36e")
151
  prompt_file = open("prompts/version_2.txt", "r").read()
152
 
153
  pinecone.init(
 
165
  "Searches and returns documents regarding the ophtal-knowledge-base.",
166
  )
167
  tools = [tool]
168
+ system_message = SystemMessage(content="You are an assistant to ophthamologists and your name is 'Dr.V AI'. Help users answer medical questions. You are supposed to answer only medical questions and not general questions.")
169
  memory_key='history'
170
 
171
  llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4", temperature=0.2)
172
+ # llm = ChatOpenAI(openai_api_key="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", model="gpt-4", temperature=0.2)
173
  prompt = OpenAIFunctionsAgent.create_prompt(
174
  system_message=system_message,
175
  extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)],
176
  )
177
 
178
+ memory = AgentTokenBufferMemory(memory_key=memory_key, llm=llm)
179
+
180
+
181
+
182
+ # agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=False, prompt=prompt, )
183
+
184
+ agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
185
+
186
+ agent_executor = AgentExecutor(
187
+ agent=agent,
188
+ tools=tools,
189
+ memory=memory,
190
+ verbose=True,
191
+ return_intermediate_steps=True,
192
+ )
193
+
194
  user_name = None
195
 
196
  def run(input_):
197
  output = agent_executor({"input": input_})
198
  output_text = output["output"]
199
+ print(output_text)
200
  source_text = ""
201
  doc_text = ""
202
 
 
226
  "documents": doc_text
227
  }
228
 
229
+ # insert_one(doc_to_insert)
230
 
231
  return output_text
232
 
233
+ # def make_conversation(message, history):
234
+ # text_ = run(message)
235
+ # for i in range(len(text_)):
236
+ # time.sleep(0.001)
237
+ # yield text_[: i+1]
238
 
239
+ # def auth_function(username, password):
240
+ # user_name = username
241
+ # return username == password
242
 
243
 
244