Spaces:
Runtime error
Runtime error
amirshafiei
commited on
Commit
•
267bd18
1
Parent(s):
e80c4af
Add app file
Browse files- api_call.py +80 -0
- app.py +107 -0
- state_handler.py +299 -0
- test_api.py +14 -0
api_call.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import requests
|
4 |
+
import requests
|
5 |
+
import urllib.parse
|
6 |
+
import json
|
7 |
+
from easydict import EasyDict as edict
|
8 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
9 |
+
from Zillow_Scraper.selenium_wrapper import search
|
10 |
+
|
11 |
+
API_token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1eG42aWVkcXlwMmtzbGVnZms2N3pmN3kyN2MzMTBsaSIsImlzcyI6ImRhdGFmaW5pdGkuY28ifQ.fwFM5-Z5ZMPwqbRuiyQhcQP9NjMqP-sm-EZ7W3BA2NnfqN6p0_Kapz6pr2tMGlZ7ranZ1DVb_gScmtPm8R_F6JpJ5dKNL7rZamUwR84TzxNPzOAns7UgjMSx-5p-7f9U2_NGg3ORveaQTfs1-QH9uiDw4v6U91lhD4rReXVgom5qhNlSbmHkj81DHVHaX969hPDtdEiQ5tspib3xgwWDyXYQYGcwPFoLIHET9srNP7zKWyghbiwh90DTnj3ASmYFO3Nrj8zdfXmkCkUXa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pfhE-7Qa1du1tZYE4PpRqTq7KysAZzfuAgkhpJzpXwDAnuEsRDSGdhbteKnRIKajiEw8jt9KW0OnXEDOxx_cU9YrPmDvJIdvQjKKYz1Tuptce7ZmxXaHYoOtzN1I1aKpGQe1UJw
|
13 |
+
format = 'JSON'
|
14 |
+
# query = 'country:US AND numRoom:3 AND numFloor:1 AND postalCode:75231 AND "prices":{"amountMax": 289000}'
|
15 |
+
download = False
|
16 |
+
request_headers = {
|
17 |
+
'Authorization': 'Bearer ' + API_token,
|
18 |
+
'Content-Type': 'application/json',
|
19 |
+
}
|
20 |
+
def send_request (budget,zip_code):
|
21 |
+
# query = "country:US AND numRoom:3 AND numFloor:1 AND postalCode:75231 AND {prices.amountMin:>=100} AND {prices.amountMax:<={param}} AND {prices.currency:USD} "
|
22 |
+
query = "country:US AND numRoom:3 AND numFloor:1 AND postalCode:"+f"{zip_code}"+" AND {prices.amountMin:>=100} AND {prices.amountMax:<="+ f"{budget}"+"} AND {prices.currency:USD} "
|
23 |
+
num_records = 7
|
24 |
+
request_data = {
|
25 |
+
'query': query,
|
26 |
+
'format': format,
|
27 |
+
'num_records': num_records,
|
28 |
+
'download': download,
|
29 |
+
}
|
30 |
+
# Make the API call.
|
31 |
+
r = requests.post('https://api.datafiniti.co/v4/properties/search',json=request_data,headers=request_headers)
|
32 |
+
if r.status_code == 200:
|
33 |
+
out_data = json.loads(r.content.decode('utf8').replace('"', '\"'))
|
34 |
+
print("Founded result: ",out_data['num_found'])
|
35 |
+
return out_data['num_found'],out_data['records']
|
36 |
+
else:
|
37 |
+
print('Request failed')
|
38 |
+
return None,None
|
39 |
+
|
40 |
+
|
41 |
+
# _,o = send_request (8000000,19701)
|
42 |
+
# print(o)
|
43 |
+
|
44 |
+
def send_zillow_request (budget,zip_code):
|
45 |
+
inf = search(zipcode=zip_code, budget=budget, for_sale = "rent")
|
46 |
+
return 1, inf
|
47 |
+
|
48 |
+
# r = requests.post('https://api.datafiniti.co/v4/properties/search',json=request_data,headers=request_headers)
|
49 |
+
# if r.status_code == 200:
|
50 |
+
# out_data = json.loads(r.content.decode('utf8').replace('"', '\"'))
|
51 |
+
# print("Founded result: ",out_data['num_found'])
|
52 |
+
# return out_data['num_found'],out_data['records']
|
53 |
+
# else:
|
54 |
+
# print('Request failed')
|
55 |
+
# return None,None
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
# ## 1. Health Check
|
61 |
+
# url = "https://api.datafiniti.co/v4/health"
|
62 |
+
# response = requests.request("GET", url, headers=[])
|
63 |
+
# print(response.text)
|
64 |
+
|
65 |
+
# ## 2. Authentication
|
66 |
+
|
67 |
+
# url = "https://api.datafiniti.co/v4/auth"
|
68 |
+
# header= {"key": "Content-Type",
|
69 |
+
# "name": "Content-Type",
|
70 |
+
# "type": "text",
|
71 |
+
# "value": "application/json"
|
72 |
+
# }
|
73 |
+
# # payload = json.dumps({
|
74 |
+
# body = {"mode": "raw","raw": "{\n\t\"email\": \"ar@playpingpong.co\",\n\t\"password\": \"p@ssWord123\"\n\t\n}"}
|
75 |
+
# response = requests.request("POST", url, headers=header, data=body)
|
76 |
+
# print(response.text)
|
77 |
+
|
78 |
+
# query = "country:US AND numRoom:3 AND numFloor:1 AND postalCode:75231 AND {prices.amountMin:>=100} AND {prices.amountMax:<= "+ f"{param}"+ "} AND {prices.currency:USD} "
|
79 |
+
|
80 |
+
|
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import gradio as gr
|
3 |
+
from random import uniform
|
4 |
+
from state_handler import state_handler
|
5 |
+
|
6 |
+
# with gr.Blocks(css=".gradio-container {text-align: right;}") as demo:
|
7 |
+
with gr.Blocks() as demo:
|
8 |
+
chatbot = gr.Chatbot()
|
9 |
+
msg = gr.Textbox()
|
10 |
+
clear = gr.ClearButton([msg, chatbot])
|
11 |
+
user_state = gr.State(value=None)
|
12 |
+
user_info = gr.State(value=None)
|
13 |
+
gathered_info = gr.State(value=None)
|
14 |
+
|
15 |
+
def user(user_message, history):
|
16 |
+
return gr.update(value=user_message, interactive=True), history + [[user_message, None]]
|
17 |
+
|
18 |
+
def respond(message, chat_history, user_state, user_info,gathered_info):
|
19 |
+
# user_state, bot_message, user_info = state_handler(user_state, message, user_info)
|
20 |
+
user_state,user_info,gathered_info = state_handler(message,user_state, user_info,gathered_info)
|
21 |
+
|
22 |
+
bot_message = user_info
|
23 |
+
|
24 |
+
time_sleep = uniform(0.004, 0.005) if len(bot_message) < 80 else uniform(0.005, 0.006)
|
25 |
+
chat_history[-1][1] = ""
|
26 |
+
for character in bot_message:
|
27 |
+
chat_history[-1][1] += character
|
28 |
+
time.sleep(time_sleep)
|
29 |
+
yield "", chat_history, user_state, user_info, gathered_info
|
30 |
+
|
31 |
+
msg.submit(
|
32 |
+
user,
|
33 |
+
[msg, chatbot],
|
34 |
+
[msg, chatbot]
|
35 |
+
).then(
|
36 |
+
respond,
|
37 |
+
[msg, chatbot, user_state, user_info, gathered_info],
|
38 |
+
[msg, chatbot, user_state, user_info, gathered_info]
|
39 |
+
).then(
|
40 |
+
lambda: gr.update(interactive=True),
|
41 |
+
None,
|
42 |
+
[msg],
|
43 |
+
queue=False
|
44 |
+
)
|
45 |
+
|
46 |
+
demo.queue()
|
47 |
+
# demo.launch() #
|
48 |
+
demo.launch() #
|
49 |
+
# demo.launch(server_name='0.0.0.0', server_port=8555,share=True) #
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
# import gradio as gr
|
64 |
+
# import requests
|
65 |
+
# import json
|
66 |
+
|
67 |
+
# with gr.Blocks(css=".gradio-container {text-align: right;}") as demo:
|
68 |
+
# chatbot = gr.Chatbot()
|
69 |
+
# msg = gr.Textbox()
|
70 |
+
# clear = gr.ClearButton([msg, chatbot])
|
71 |
+
# user_state = gr.State(value=0)
|
72 |
+
|
73 |
+
# def send_request(msg):
|
74 |
+
# url = "http://localhost:5005/webhooks/rest/webhook"
|
75 |
+
# # url = "http://192.168.1.170:9901/webhooks/rest/webhook"
|
76 |
+
|
77 |
+
# payload = json.dumps({
|
78 |
+
# "sender": "test_user",
|
79 |
+
# "message": msg,
|
80 |
+
# "platform_type": "telegram",
|
81 |
+
# "metadata": {}
|
82 |
+
# })
|
83 |
+
# headers = {
|
84 |
+
# 'Content-Type': 'application/json'
|
85 |
+
# }
|
86 |
+
|
87 |
+
# return requests.request("POST", url, headers=headers, data=payload)
|
88 |
+
|
89 |
+
# def respond(message, chat_history):
|
90 |
+
# responses = send_request(message)
|
91 |
+
# if responses.status_code != 200:
|
92 |
+
# return "خطا در اتصال"
|
93 |
+
# responses = responses.json()
|
94 |
+
# bot_message = "\n\n".join([response['text'] for response in responses])
|
95 |
+
# # for response in responses:
|
96 |
+
# # bot_message = response['text']
|
97 |
+
|
98 |
+
# chat_history.append((message, bot_message))
|
99 |
+
|
100 |
+
# return "", chat_history
|
101 |
+
|
102 |
+
# msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
103 |
+
|
104 |
+
# demo.launch(
|
105 |
+
# server_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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from langchain.chains.router import MultiPromptChain
|
3 |
+
from langchain.chains.router.llm_router import LLMRouterChain,RouterOutputParser
|
4 |
+
# from langchain.prompts import PromptTemplate
|
5 |
+
from langchain.prompts import ChatPromptTemplate
|
6 |
+
from langchain import OpenAI, LLMChain, PromptTemplate
|
7 |
+
from langchain.memory import ConversationBufferMemory
|
8 |
+
import os
|
9 |
+
import openai
|
10 |
+
from langchain.chat_models import ChatOpenAI
|
11 |
+
from api_call import send_request , send_zillow_request
|
12 |
+
from langchain.llms import LlamaCpp
|
13 |
+
from langchain.callbacks.manager import CallbackManager
|
14 |
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
15 |
+
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
16 |
+
|
17 |
+
# questions = {
|
18 |
+
# 'buy house or loan': 'Do you want to buy a house or loan for it?',
|
19 |
+
# 'zip code':'Could you please provide me with the zip code of the area where you are looking to buy a home?',
|
20 |
+
# 'home feature' : 'Can you describe the desired features of your dream home and your goals',
|
21 |
+
# 'budget' : 'What is your budget for buying a home?',
|
22 |
+
# 'first time buyer' : 'Are you a first-time buyer?',
|
23 |
+
# 'introduce' : 'Hi. I\'m here to assist you with buying a home or getting a loan. Could you please provide me with some information to help you better?',
|
24 |
+
# 'ignored' : 'I don\'t understand. Could you please rephrase your question?',
|
25 |
+
# 'ask_question' : 'Could you please provide me with the zip code of the area where you are looking to buy a home?'
|
26 |
+
# }
|
27 |
+
|
28 |
+
|
29 |
+
# questions = {
|
30 |
+
# 'buy house or loan': 'Which one are you more interested in? Buy a house or loan for it?',
|
31 |
+
# 'zip code':'Could you please provide me with the zip code of the area where you are looking to buy a home?',
|
32 |
+
# 'home feature' : 'Can you describe the desired features of your dream home and your goals',
|
33 |
+
# 'budget' : 'What is your budget for buying a home?',
|
34 |
+
# 'first time buyer' : 'Are you a first-time buyer?',
|
35 |
+
# 'introduce' : 'Hi I’m Samar, your real state assistant. In 60 seconds I can help you find a house or how to save $500 on your loans.',
|
36 |
+
# 'ignored' : 'I don\'t understand. Could you please rephrase your question?',
|
37 |
+
# 'ask_question' : 'Could you please provide me with the zip code of the area where you are looking to buy a home?'
|
38 |
+
# }
|
39 |
+
|
40 |
+
|
41 |
+
questions = {
|
42 |
+
'buy house or loan': 'Are you currently in the market to purchase or rent a home?',
|
43 |
+
'zip code':'Could you please provide me with the zip code of the area where you are looking to buy a home?',
|
44 |
+
'home feature' : 'Can you describe the desired features of your dream home and your goals',
|
45 |
+
'budget' : 'What is your budget for buying a home?',
|
46 |
+
'first time buyer' : 'Are you a first-time buyer?',
|
47 |
+
'introduce' : 'Hi, this is Samar from Royal Real State Agency. I hope you\'re doing well! I wanted to reach out because \
|
48 |
+
I noticed your interest in real estate and thought I could assist you in finding the perfect home.',
|
49 |
+
'ignored' : 'I don\'t understand. Could you please rephrase your question?',
|
50 |
+
'ask_question' : 'Could you please provide me with the zip code of the area where you are looking to buy a home?'
|
51 |
+
}
|
52 |
+
|
53 |
+
def init_chain():
|
54 |
+
|
55 |
+
budget_template = """ You are a compressor that get a question and answer aboute money/budget and extremly compress the answer into a number and return an integer number.
|
56 |
+
example: if input=600k then output=600000
|
57 |
+
Here is the question :
|
58 |
+
{question}
|
59 |
+
Here is the answer :
|
60 |
+
{answer}"""
|
61 |
+
|
62 |
+
zipcode_templet = """ You are a compressor that get a question and answer aboute zip code and extremly compress the answer into a number and return only a number.
|
63 |
+
Here is the question :
|
64 |
+
{question}
|
65 |
+
Here is the answer :
|
66 |
+
{answer}"""
|
67 |
+
|
68 |
+
feature_template = """ You are a compressor that get a question and answer about desiered home feature and extract home feature from the answer into a short term.
|
69 |
+
Here is the question :
|
70 |
+
{question}
|
71 |
+
Here is the answer :
|
72 |
+
{answer}"""
|
73 |
+
|
74 |
+
buy_loan_template = """ You are a compressor that get a question and answer a bout buy house or loan, and extremly compress the answer into short term.
|
75 |
+
Here is the question :
|
76 |
+
{question}
|
77 |
+
Here is the answer :
|
78 |
+
{answer}"""
|
79 |
+
|
80 |
+
first_buyer_template = """ You are a compressor that get a question and answer a bout buy house or loan, and extremly compress the answer into short term.
|
81 |
+
Here is the question :
|
82 |
+
{question}
|
83 |
+
Here is the answer :
|
84 |
+
{answer}"""
|
85 |
+
|
86 |
+
home_feature_template = """ You are a prompt generator to generate a sentence to describe a home property \
|
87 |
+
for a buyer based on the input_data. for example describe prices, floorSizeValue,numRoom \
|
88 |
+
numFloor, numBedroom, neighborhoods, floorSizeValue, feature item of input_data.
|
89 |
+
Here is the input_data :
|
90 |
+
{input_data}
|
91 |
+
"""
|
92 |
+
|
93 |
+
cat_task_template = """ You are a classifier to assign input_message into one of the below categoryis. \
|
94 |
+
categoryis Item: \
|
95 |
+
- `buy house or loan `: (example: Are you currently in the market to purchase or rent a home? yes. buy a house) \
|
96 |
+
- `budget`: (example: What is your budget for buying a home? 600k or 5000$ or 8000 or I have 36000$ money) \
|
97 |
+
- `first time buyer`: (example: Are you a first-time buyer? yes) \
|
98 |
+
- `zip code` : (example: Could you please provide me with the zip code of the area you are interested in? 19701 , 85412 , ...)
|
99 |
+
- `home feature' : (example : Can you describe the desired features of your dream home and your goals? 2 rooms)
|
100 |
+
- `ignored` : a message that don't related to any question and is a unusaul message.
|
101 |
+
|
102 |
+
Here is the input_message and question :
|
103 |
+
{input_message}
|
104 |
+
output:
|
105 |
+
return the detected category.
|
106 |
+
"""
|
107 |
+
prompt_infos = [
|
108 |
+
{
|
109 |
+
"name": "budget",
|
110 |
+
"prompt_template": budget_template
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"name": "zip code",
|
114 |
+
"prompt_template": zipcode_templet
|
115 |
+
},
|
116 |
+
{
|
117 |
+
"name": "home feature",
|
118 |
+
"prompt_template": feature_template
|
119 |
+
},
|
120 |
+
{
|
121 |
+
"name": "buy house or loan",
|
122 |
+
"prompt_template": buy_loan_template
|
123 |
+
},
|
124 |
+
{
|
125 |
+
"name": "first time buyer",
|
126 |
+
"prompt_template": first_buyer_template
|
127 |
+
},
|
128 |
+
{
|
129 |
+
"name": "home_feature",
|
130 |
+
"prompt_template": home_feature_template
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"name": "category",
|
134 |
+
"prompt_template": cat_task_template
|
135 |
+
},
|
136 |
+
]
|
137 |
+
destination_chains = {}
|
138 |
+
for p_info in prompt_infos:
|
139 |
+
name = p_info["name"]
|
140 |
+
prompt_template = p_info["prompt_template"]
|
141 |
+
prompt = ChatPromptTemplate.from_template(template=prompt_template,)
|
142 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
143 |
+
destination_chains[name] = chain
|
144 |
+
return destination_chains
|
145 |
+
|
146 |
+
|
147 |
+
#Age + Pricing
|
148 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
149 |
+
llm = ChatOpenAI(temperature=0.0)
|
150 |
+
# llm = LlamaCpp(
|
151 |
+
# model_path="/home/yaghoubian/yaghoubian/fast_avatar/hrviton/ControlNet-v1-1-nightly/lang_chain/aa/llama-2-7b-chat.ggmlv3.q6_K.bin",
|
152 |
+
# input={"temperature": 0.1, "max_length": 2000, "top_p": 1},
|
153 |
+
# callback_manager=callback_manager,)
|
154 |
+
|
155 |
+
# prompt_instruction = """
|
156 |
+
# Instructions: you are a classifier for classify input message into one of the below categoryis. \
|
157 |
+
# categoryis Item: \
|
158 |
+
# -`math_question` \
|
159 |
+
# -`Historical` \
|
160 |
+
# Here is the question: what is 1+1? \
|
161 |
+
# """
|
162 |
+
# print("before a")
|
163 |
+
# a = llm(prompt_instruction)
|
164 |
+
# print(a)
|
165 |
+
|
166 |
+
chains = init_chain()
|
167 |
+
|
168 |
+
def state_handler(message,user_state=None, user_info=None,gathered_info=None):
|
169 |
+
if user_info == None:
|
170 |
+
print("new_user")
|
171 |
+
user_state = ['introduce','buy house or loan','zip code','home feature','budget','first time buyer']
|
172 |
+
user_info = questions[user_state[0]] + "\n" + questions[user_state[1]]
|
173 |
+
gathered_info = {}
|
174 |
+
user_state.remove('introduce')
|
175 |
+
return user_state,user_info,gathered_info
|
176 |
+
else:
|
177 |
+
assigned_classes = category(input_message = user_info.split('\n')[-1] + " " + message)
|
178 |
+
|
179 |
+
for assigned_class in assigned_classes :
|
180 |
+
Short_response, user_state= compress_response(input_message=message,input_question=questions[assigned_class],\
|
181 |
+
user_state=user_state,assigned_class=assigned_class,user_info=user_info)
|
182 |
+
print (f"{assigned_class} : {Short_response}")
|
183 |
+
gathered_info[assigned_class] = Short_response
|
184 |
+
|
185 |
+
if len(user_state)>0:
|
186 |
+
if 'rephrase your answer for this question' in Short_response:
|
187 |
+
user_info = Short_response
|
188 |
+
else :
|
189 |
+
user_info = questions[user_state[0]]
|
190 |
+
else:
|
191 |
+
res,response = send_request(gathered_info['budget'],gathered_info['zip code'])
|
192 |
+
# print("Start Zillow scraping. ")
|
193 |
+
# res,response = send_zillow_request(gathered_info['budget'],gathered_info['zip code'])
|
194 |
+
if response == None:
|
195 |
+
user_info = "Sorry, there is an error in searching result. please try again."
|
196 |
+
else:
|
197 |
+
if res>0:
|
198 |
+
user_info = f"This is your information: \n{gathered_info}. \n we find {res} results." #\
|
199 |
+
# \n Here is the result specification: \n {response}."
|
200 |
+
for idx,i in enumerate(response):
|
201 |
+
in_feature =f" numBathroom: {i['numBathroom']} , numRoom: {i['numRoom']}, numFloor: {i['numFloor']}, yearBuilt:{i['yearBuilt']}, floorSizeValue: {i['floorSizeValue']} {i['floorSizeUnit']} "
|
202 |
+
home_feature_prompt = chains['home_feature'].run(input_data=in_feature)
|
203 |
+
print("home_feature_prompt: ",home_feature_prompt)
|
204 |
+
try:
|
205 |
+
user_info = user_info + f"\n{idx+1}- " +home_feature_prompt + f"\n {str(i['mostRecentPriceSourceURL'])} \n"
|
206 |
+
except:
|
207 |
+
user_info = user_info + f"\n{idx+1}- " +home_feature_prompt
|
208 |
+
|
209 |
+
else :
|
210 |
+
user_info = f"This is your information: \n{gathered_info}. \n Sorry. we can't find \
|
211 |
+
any case by the entered budget and zip code"
|
212 |
+
|
213 |
+
return user_state,user_info,gathered_info
|
214 |
+
|
215 |
+
|
216 |
+
def category(input_message):
|
217 |
+
|
218 |
+
# os.environ["OPENAI_API_KEY"] = "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"
|
219 |
+
# llm = ChatOpenAI(temperature=0.1)
|
220 |
+
|
221 |
+
# # You are a chatbot. you must read the input message and \
|
222 |
+
# # tag or categorize it to one of the below item
|
223 |
+
|
224 |
+
# new_task_template = """ You are a classifier to assign input_message into one (or more than one) of the below categoryis.\
|
225 |
+
# categoryis Item: \
|
226 |
+
# - `buy house or loan `: (example: do you want to buy a house ot loan for it? buy a house) \
|
227 |
+
# - `budget`: (example: What is your budget for buying a home? 5000$ or 8000 or I have 36000$ money) \
|
228 |
+
# - `first time buyer`: (example: Are you a first-time buyer? yes) \
|
229 |
+
# - `zip code` : (example: Could you please provide me with the zip code of the area you are interested in? 8542)
|
230 |
+
# - `home feature' : (example : Can you describe the desired features of your dream home and your goals? 2 rooms)
|
231 |
+
# - `introduce` : (example: Hi. I'm here to assist you with buying a home or getting a loan.)
|
232 |
+
# - `ignored` : a message that don't related to any question and is a unusaul message.
|
233 |
+
# - `ask_question` : (example: Could you please provide me with some information to help you better? Sure.).
|
234 |
+
|
235 |
+
# Here is the input message :
|
236 |
+
# {input_message}
|
237 |
+
# output:
|
238 |
+
# return the detected category.
|
239 |
+
# """
|
240 |
+
# prompt = ChatPromptTemplate.from_template(template=new_task_template,)
|
241 |
+
# cat_chain = LLMChain(llm=llm, prompt=prompt)
|
242 |
+
|
243 |
+
classe_list = ["buy house or loan","budget","first time buyer","zip code","home feature"]
|
244 |
+
|
245 |
+
message_classes = chains['category'].run(input_message=input_message)
|
246 |
+
|
247 |
+
detected_classes = []
|
248 |
+
for i in classe_list:
|
249 |
+
if i in message_classes.lower():
|
250 |
+
detected_classes.append(i)
|
251 |
+
print("detected_classes: ",detected_classes)
|
252 |
+
return detected_classes
|
253 |
+
|
254 |
+
|
255 |
+
def compress_response(input_message,input_question,user_state,assigned_class=None,user_info=None):
|
256 |
+
|
257 |
+
# new_task_template = """ You are a compressor that get a question and answer and extremly compress the answer into short term. \
|
258 |
+
# Here is the question :
|
259 |
+
# {question}
|
260 |
+
# Here is the answer :
|
261 |
+
# {answer}
|
262 |
+
# """
|
263 |
+
# prompt = ChatPromptTemplate.from_template(template=new_task_template)
|
264 |
+
# chain = LLMChain(llm=llm, prompt=prompt)
|
265 |
+
|
266 |
+
if "buy house or loan"in assigned_class.lower():
|
267 |
+
user_state.remove('buy house or loan')
|
268 |
+
chain = chains[assigned_class]
|
269 |
+
response = chain.run(question=input_question , answer=input_message)
|
270 |
+
elif 'zip code'in assigned_class.lower():
|
271 |
+
user_state.remove('zip code')
|
272 |
+
chain = chains[assigned_class]
|
273 |
+
response = chain.run(question=input_question , answer=input_message)
|
274 |
+
elif 'home feature'in assigned_class.lower():
|
275 |
+
user_state.remove('home feature')
|
276 |
+
chain = chains[assigned_class]
|
277 |
+
response = chain.run(question=input_question , answer=input_message)
|
278 |
+
elif 'budget' in assigned_class.lower():
|
279 |
+
user_state.remove('budget')
|
280 |
+
chain = chains[assigned_class]
|
281 |
+
response = chain.run(question=input_question , answer=input_message)
|
282 |
+
elif 'first time buyer' in assigned_class.lower():
|
283 |
+
user_state.remove('first time buyer')
|
284 |
+
chain = chains[assigned_class]
|
285 |
+
response = chain.run(question=input_question , answer=input_message)
|
286 |
+
elif 'ignored' in assigned_class.lower():
|
287 |
+
# user_state.remove('ignored')
|
288 |
+
response = f"I can't understand your answer. please rephrase your answer for this question. \n {user_info}"
|
289 |
+
|
290 |
+
return response ,user_state
|
291 |
+
|
292 |
+
|
293 |
+
|
294 |
+
|
295 |
+
|
296 |
+
|
297 |
+
# Great. You can see this website to see the house feature.
|
298 |
+
|
299 |
+
# https://www.zillow.com/homes/22201-wayside-Mission-viej-CA92692_ib/25614382_zpid
|
test_api.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
url = "http://0.0.0.0:8000/run_fully_automated?input_image&prompt&room_class"
|
4 |
+
|
5 |
+
# Livingroom Payloads
|
6 |
+
l_payload1 = {"zipcode" : "19701",
|
7 |
+
"budget":"800K",
|
8 |
+
"for_sale":"rent"}
|
9 |
+
response = requests.request("POST", url, data=l_payload1)
|
10 |
+
print(response.text)
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|