lakshmivairamani commited on
Commit
2342013
1 Parent(s): a1839b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -49
app.py CHANGED
@@ -1,63 +1,187 @@
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  )
60
 
 
 
61
 
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
+ import os
2
+ import requests
3
  import gradio as gr
 
4
 
5
+ from langchain.memory import ConversationBufferMemory # Updated import
 
 
 
6
 
7
+ from langchain_core.prompts import ChatPromptTemplate
8
+ from langchain_community.utilities import SQLDatabase
9
+ from langchain_core.output_parsers import StrOutputParser
10
+ from langchain_core.runnables import RunnablePassthrough
11
+ from langchain_openai import ChatOpenAI
12
+ from langchain.agents import create_tool_calling_agent, AgentExecutor, Tool
13
+ from langchain.vectorstores import FAISS
14
 
15
+ from langchain_openai import OpenAIEmbeddings
16
+ from langchain.text_splitter import CharacterTextSplitter
17
+ from PyPDF2 import PdfReader
 
 
 
 
 
 
18
 
19
+ # Initialize the memory
 
 
 
 
20
 
21
+ memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
22
 
 
23
 
 
 
 
 
 
 
 
 
24
 
25
+ open_api_key_token = userdata.get('OPENAI_API_KEY')
 
26
 
27
+ open_weather_api_key = userdata.get('OPEN_WEATHER_API_KEY')
28
+
29
+ os.environ['OPENAI_API_KEY'] = open_api_key_token
30
+ db_uri = 'postgresql+psycopg2://postgres:[email protected]:5432/warehouse'
31
+ # Database setup
32
+
33
+ db = SQLDatabase.from_uri(db_uri)
34
+
35
+ # LLM setup
36
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
37
+
38
+ # Define the SQL query generation tool
39
+ template_query_generation = """Based on the table schema below, write a SQL query that would answer the user's question:
40
+ {schema}
41
+
42
+ Question: {question}
43
+ SQL Query:"""
44
+ prompt_query_generation = ChatPromptTemplate.from_template(template_query_generation)
45
+
46
+ def get_schema(_):
47
+ return db.get_table_info()
48
+
49
+ def generate_sql_query(question):
50
+ schema = get_schema(None)
51
+ input_data = {"question": question}
52
+ sql_chain = (RunnablePassthrough.assign(schema=get_schema)
53
+ | prompt_query_generation
54
+ | llm.bind(stop="\n SQL Result:")
55
+ | StrOutputParser()
56
+ )
57
+ return sql_chain.invoke(input_data)
58
+
59
+ def run_query(query):
60
+ return db.run(query)
61
+
62
+ # Define the database query tool
63
+ def database_tool(question):
64
+ sql_query = generate_sql_query(question)
65
+ return run_query(sql_query)
66
+
67
+ # Define the weather data retrieval tool
68
+ def get_weather_data(location="United Arab Emirates"):
69
+ api_key = open_weather_api_key
70
+ base_url = "http://api.openweathermap.org/data/2.5/weather?"
71
+
72
+ if location is None or location.strip() == "":
73
+ location = "United Arab Emirates"
74
+
75
+ complete_url = f"{base_url}q={location}&appid={api_key}&units=metric"
76
+
77
+ response = requests.get(complete_url)
78
+ data = response.json()
79
+
80
+ if data["cod"] != "404":
81
+ main = data["main"]
82
+ weather_description = data["weather"][0]["description"]
83
+ temperature = main["temp"]
84
+ return f"The current temperature in {location} is {temperature}°C with {weather_description}."
85
+ else:
86
+ return "Weather data is not found."
87
+ #get_weather_data("United Arab Emirates")
88
+
89
+ # Define the document data tool
90
+ def load_and_split_pdf(pdf_path):
91
+ reader = PdfReader(pdf_path)
92
+ text = ''
93
+ for page in reader.pages:
94
+ text += page.extract_text()
95
+
96
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
97
+ texts = text_splitter.split_text(text)
98
+ return texts
99
+
100
+ def create_vector_store(texts):
101
+ embeddings = OpenAIEmbeddings()
102
+ vector_store = FAISS.from_texts(texts, embeddings)
103
+ return vector_store
104
+
105
+ def query_vector_store(vector_store, query):
106
+ docs = vector_store.similarity_search(query)
107
+ return '\n\n'.join([doc.page_content for doc in docs])
108
+
109
+ # Load and process the PDF (ensure the PDF is accessible from your Colab environment)
110
+ pdf_path = "/content/drive/My Drive/Colab Notebooks/LLM/data/PDF/medical_information.pdf"
111
+ # Check if the user has the necessary permissions to access the directory
112
+ # if not os.path.isdir(pdf_path):
113
+ # raise ValueError(f"Directory {pdf_path} does not exist or you do not have permission to access it.")
114
+
115
+ texts = load_and_split_pdf(pdf_path)
116
+ vector_store = create_vector_store(texts)
117
+
118
+ def document_data_tool(query):
119
+ return query_vector_store(vector_store, query)
120
+
121
+ # Initialize the agent with the tools
122
+ tools = [
123
+ Tool(name="DatabaseQuery", func=database_tool, description="Tool to query the database based on the user's question. Only handles questions related to the collegedb schema, including tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook. Ensure to use only the available fields in these tables.", tool_choice="required"),
124
+ Tool(name="WeatherData", func=get_weather_data, description="Tool to get current weather data for a city or country. Handles questions related to current weather conditions in specific cities or countries.", tool_choice="required"),
125
+ Tool(name="DocumentData", func=document_data_tool, description="Tool to search and retrieve information from the uploaded document.", tool_choice="required"),
126
+ ]
127
+
128
+ prompt_template = f"""You are an assistant that helps with database queries, weather information, and document retrieval.
129
+ For SQL database-related questions, only use the fields available in the collegedb schema, which includes tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook.
130
+ For weather-related questions, if the user specifies a city, provide the weather information for that city. If the user specifies a country or does not specify a city, provide the weather information for the specified country or the default location of 'United Arab Emirates'.
131
+ For document-related questions, search and retrieve information from the uploaded document.
132
+ {{agent_scratchpad}}
133
+ Question: {{input}}
134
+ {memory.buffer}
135
  """
136
+
137
+ prompt = ChatPromptTemplate.from_template(prompt_template)
138
+
139
+ # Initialize the agent with memory
140
+ llm_with_memory = llm.bind(memory=memory)
141
+ agent = create_tool_calling_agent(llm_with_memory, tools, prompt)
142
+ agent_executor = AgentExecutor(agent=agent, tools=tools, memory= memory, verbose=True)
143
+
144
+ # Define the interface function
145
+ max_iterations = 5
146
+ iterations = 0
147
+
148
+ def answer_question(user_question, city="United Arab Emirates"):
149
+ global iterations
150
+ iterations = 0
151
+
152
+ while iterations < max_iterations:
153
+ response = agent_executor.invoke({"input": user_question})
154
+ if isinstance(response, dict):
155
+ response_text = response.get("output", "")
156
+ else:
157
+ response_text = response
158
+ if "invalid" not in response_text.lower():
159
+ break
160
+ iterations += 1
161
+
162
+ if iterations == max_iterations:
163
+ return "The agent could not generate a valid response within the iteration limit."
164
+
165
+ # Print memory buffer for debugging
166
+ print("Memory Buffer:", memory.buffer)
167
+ # Print memory buffer for debugging
168
+ print("Memory Buffer11:", memory.load_memory_variables({}))
169
+
170
+
171
+ # Format the response text
172
+ response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
173
+
174
+ return response_text
175
+
176
+ # Create the Gradio interface
177
+ iface = gr.Interface(
178
+ fn=answer_question,
179
+ inputs="text",
180
+ outputs="text",
181
+ title="Chat with Database and Weather",
182
+ description="Ask a question about the database and get a response in natural language, including current weather information."
183
  )
184
 
185
+ # Launch the Gradio interface
186
+ iface.launch(share=True, debug=True)
187