Spaces:
Runtime error
Runtime error
zachpaul38
commited on
Commit
•
85c0c49
1
Parent(s):
426e556
Add application file
Browse files- .DS_Store +0 -0
- Dockerfile +9 -0
- README.md +1 -12
- app.py +97 -0
- flagged/log.csv +2 -0
- requirements.txt +7 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
Dockerfile
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM public.ecr.aws/lambda/python:3.9
|
2 |
+
|
3 |
+
COPY app.py ${LAMBDA_TASK_ROOT}
|
4 |
+
COPY requirements.txt ./
|
5 |
+
|
6 |
+
RUN pip install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
|
7 |
+
RUN chmod -R o+rX .
|
8 |
+
|
9 |
+
CMD ["app.lambda_handler"]
|
README.md
CHANGED
@@ -1,12 +1 @@
|
|
1 |
-
|
2 |
-
title: Edai Gradio Langchain
|
3 |
-
emoji: 🏢
|
4 |
-
colorFrom: purple
|
5 |
-
colorTo: red
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.34.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
# edai_gradio_test
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
|
3 |
+
from langchain.agents import create_sql_agent
|
4 |
+
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
|
5 |
+
from langchain.sql_database import SQLDatabase
|
6 |
+
from langchain.llms.openai import OpenAI
|
7 |
+
# from langchain.agents import AgentExecutor
|
8 |
+
import openai
|
9 |
+
import json
|
10 |
+
import gradio as gr
|
11 |
+
import boto3
|
12 |
+
|
13 |
+
|
14 |
+
from langchain import OpenAI, AgentExecutor, Tool, load_tools, initialize_agent
|
15 |
+
from langchain.agents import AgentType
|
16 |
+
from langchain.agents.tools import SQLAgent
|
17 |
+
from langchain.memory import ReadOnlySharedMemory
|
18 |
+
|
19 |
+
|
20 |
+
postgres_connection_str = os.environ['POSTGRES_CONNECTION_STR']
|
21 |
+
access_key = os.environ['AWS_ACCESS_KEY_ID']
|
22 |
+
secret_key = os.environ['AWS_SECRET_ACCESS_KEY']
|
23 |
+
openai_api_key = os.environ['OPENAI_API_KEY']
|
24 |
+
region= 'us-east-1'
|
25 |
+
|
26 |
+
db = SQLDatabase.from_uri(postgres_connection_str,schema='langchain_testing')
|
27 |
+
s3_client = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key,region_name=region)
|
28 |
+
llm = OpenAI(temperature=0, verbose=True, openai_api_key=openai_api_key)
|
29 |
+
|
30 |
+
# toolkit = SQLDatabaseToolkit(db=db, llm=llm)
|
31 |
+
# agent_executor = create_sql_agent(
|
32 |
+
# llm=OpenAI(temperature=0),
|
33 |
+
# toolkit=toolkit,
|
34 |
+
# verbose=True
|
35 |
+
# )
|
36 |
+
|
37 |
+
# def generate_response(question):
|
38 |
+
# prompt_template = """
|
39 |
+
# Keep in mind that any site, building, or property related question should be routed to the real estate portal.
|
40 |
+
# Any Local or city or city-sector incentives programs are asking about the local incentives program table.
|
41 |
+
# Any State incentives programs are asking about the state incentives program table.
|
42 |
+
# When the user asks about a state incentive, don't query the local_incentives_catalog table.
|
43 |
+
# When the user asks about a local/city incentive, don't query the local_incentives_catalog table.
|
44 |
+
# If you can't find the answer, make sure to look up the program field in the local and state incentives catalogs.
|
45 |
+
# If your final answer is "I don't know", then respond with "Please adjust your question and try asking again."
|
46 |
+
|
47 |
+
# """
|
48 |
+
# chain_response = agent_executor.run(question + prompt_template)
|
49 |
+
# # bucket_name = 'your-bucket-name'
|
50 |
+
# # file_name = 'flagged_text.txt'
|
51 |
+
# # s3_client.put_object(Body=input_text, Bucket=bucket_name, Key=file_name)
|
52 |
+
|
53 |
+
# return chain_response
|
54 |
+
|
55 |
+
# iface = gr.Interface(
|
56 |
+
# fn=generate_response,
|
57 |
+
# inputs=gr.inputs.Textbox(label='Enter your question:', default='What is the Neighborhood Credit Fund in New York City?'),
|
58 |
+
# outputs=gr.outputs.Textbox(label="EDai Analyst's Response:")
|
59 |
+
# )
|
60 |
+
|
61 |
+
# iface.launch(share=True)
|
62 |
+
|
63 |
+
# Load the language model and tools
|
64 |
+
llm = OpenAI()
|
65 |
+
tools = load_tools(["llama_index"], llm=llm)
|
66 |
+
|
67 |
+
# Define the agent
|
68 |
+
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)
|
69 |
+
|
70 |
+
# Define the Gradio interface
|
71 |
+
def chatbot(user_name):
|
72 |
+
# Define the initial message
|
73 |
+
message = f"Hi {user_name}, how may I help you?"
|
74 |
+
# Define the memory
|
75 |
+
memory = ReadOnlySharedMemory()
|
76 |
+
# Define the SQL agent
|
77 |
+
sql_agent = SQLAgent(database="my_database.db", table="my_table")
|
78 |
+
# Loop until the user ends the conversation
|
79 |
+
|
80 |
+
while True:
|
81 |
+
# Get the user's input
|
82 |
+
user_input = gr.text_area(message, default_value="")
|
83 |
+
|
84 |
+
# If the user ends the conversation, break the loop
|
85 |
+
if user_input.lower() in ["bye", "goodbye", "exit"]:
|
86 |
+
break
|
87 |
+
# Get the answer from the SQL agent
|
88 |
+
answer = sql_agent.run(user_input, memory=memory)
|
89 |
+
# Ask if the user needs anything else
|
90 |
+
message = f"{answer}\n\nDo you need anything else?"
|
91 |
+
# Return the final message
|
92 |
+
return "Goodbye!"
|
93 |
+
|
94 |
+
# Define the Gradio interface
|
95 |
+
iface = gr.Interface(fn=chatbot, inputs=["text"], outputs="text", title="LangChain Chatbot", description="A chatbot powered by LangChain and Gradio.")
|
96 |
+
# Launch the interface
|
97 |
+
iface.launch()
|
flagged/log.csv
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Enter your question:,EDai Analyst's Response:,flag,username,timestamp
|
2 |
+
What is the Neighborhood Credit Fund in New York City?,The Neighborhood Credit Fund provides competitive interest rate loans to small and emerging businesses in New York City.,,,2023-05-30 16:34:18.482490
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests==2.27.1
|
2 |
+
pandas==1.4.2
|
3 |
+
psycopg2-binary==2.9.3
|
4 |
+
boto3==1.21.32
|
5 |
+
openai==0.27.7
|
6 |
+
langchain==0.0.184
|
7 |
+
gradio==3.32.0
|