Spaces:
Sleeping
Sleeping
ruchi
commited on
Commit
·
0c6560f
1
Parent(s):
4331889
Money needs integrated with utils
Browse files- app.py +4 -0
- db.py +36 -0
- money_needs.sqlite +0 -0
- requirements.txt +2 -1
- utils.py +84 -0
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import google.generativeai as genai
|
|
| 4 |
GOOGLE_API_KEY=os.getenv('GEMINI_API_KEY')
|
| 5 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 6 |
model = genai.GenerativeModel(model_name = "gemini-pro")
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
# Create a banner using Markdown
|
|
@@ -86,6 +87,9 @@ if submit_button:
|
|
| 86 |
proposal = proposal.format(selectedCity, selectedProduct, userProposal)
|
| 87 |
st.write("Entered proposal:", proposal)
|
| 88 |
st.write("Analyzing your proposition")
|
|
|
|
|
|
|
|
|
|
| 89 |
response = model.generate_content([pre_prompt.format(proposal)])
|
| 90 |
st.write(response.text)
|
| 91 |
|
|
|
|
| 4 |
GOOGLE_API_KEY=os.getenv('GEMINI_API_KEY')
|
| 5 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 6 |
model = genai.GenerativeModel(model_name = "gemini-pro")
|
| 7 |
+
from utils import findTop3MoneyNeeds
|
| 8 |
|
| 9 |
|
| 10 |
# Create a banner using Markdown
|
|
|
|
| 87 |
proposal = proposal.format(selectedCity, selectedProduct, userProposal)
|
| 88 |
st.write("Entered proposal:", proposal)
|
| 89 |
st.write("Analyzing your proposition")
|
| 90 |
+
|
| 91 |
+
topMoneyNeeds = findTop3MoneyNeeds(moneyNeeds)
|
| 92 |
+
|
| 93 |
response = model.generate_content([pre_prompt.format(proposal)])
|
| 94 |
st.write(response.text)
|
| 95 |
|
db.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
def fetch_db_rows_as_dicts(db_path, table_name):
|
| 4 |
+
try:
|
| 5 |
+
# Connect to the SQLite database
|
| 6 |
+
conn = sqlite3.connect(db_path)
|
| 7 |
+
conn.row_factory = sqlite3.Row # This allows us to access columns by name
|
| 8 |
+
cursor = conn.cursor()
|
| 9 |
+
|
| 10 |
+
# Get the column names
|
| 11 |
+
cursor.execute(f"PRAGMA table_info({table_name});")
|
| 12 |
+
columns_info = cursor.fetchall()
|
| 13 |
+
column_names = [col[1] for col in columns_info]
|
| 14 |
+
|
| 15 |
+
# Print the column names
|
| 16 |
+
print("Column names:", column_names)
|
| 17 |
+
|
| 18 |
+
# Execute a query to fetch all rows from the table
|
| 19 |
+
cursor.execute(f"SELECT * FROM {table_name};")
|
| 20 |
+
rows = cursor.fetchall()
|
| 21 |
+
|
| 22 |
+
assert len(rows) > 1
|
| 23 |
+
|
| 24 |
+
return column_names, rows[1:]
|
| 25 |
+
|
| 26 |
+
except sqlite3.Error as e:
|
| 27 |
+
print(f"SQLite error: {e}")
|
| 28 |
+
finally:
|
| 29 |
+
# Close the connection
|
| 30 |
+
if conn:
|
| 31 |
+
conn.close()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Example usage:
|
| 36 |
+
# fetch_db_rows_as_dicts('money_needs.sqlite', 'money_needs')
|
money_needs.sqlite
ADDED
|
Binary file (8.19 kB). View file
|
|
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
streamlit
|
| 2 |
altair==4.0
|
| 3 |
transformers
|
| 4 |
-
google-generativeai
|
|
|
|
|
|
| 1 |
streamlit
|
| 2 |
altair==4.0
|
| 3 |
transformers
|
| 4 |
+
google-generativeai
|
| 5 |
+
db-sqlite3
|
utils.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from db import fetch_db_rows_as_dicts
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
GOOGLE_API_KEY='AIzaSyC0T2fN5Dga-6nkPc6HYV7-bDZskqgALX0'
|
| 6 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 7 |
+
model = genai.GenerativeModel(model_name = "gemini-pro")
|
| 8 |
+
|
| 9 |
+
def load_json_from_string(json_string):
|
| 10 |
+
try:
|
| 11 |
+
data = json.loads(json_string)
|
| 12 |
+
return data
|
| 13 |
+
except json.JSONDecodeError as e:
|
| 14 |
+
print(f"Error decoding JSON: {e}")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"An error occurred: {e}")
|
| 17 |
+
|
| 18 |
+
def concatenate_keys(keys):
|
| 19 |
+
concatenated_string = ""
|
| 20 |
+
for i, d in enumerate(keys, start=1):
|
| 21 |
+
concatenated_string += f"{d} "
|
| 22 |
+
return concatenated_string.strip()
|
| 23 |
+
|
| 24 |
+
def transform_to_dict_of_dicts(columns, rows):
|
| 25 |
+
# Initialize the result dictionary
|
| 26 |
+
result = {}
|
| 27 |
+
|
| 28 |
+
# Iterate over each row
|
| 29 |
+
for row in rows:
|
| 30 |
+
# The first element of the row is the key for the outer dictionary
|
| 31 |
+
outer_key = row[0].strip()
|
| 32 |
+
|
| 33 |
+
# Initialize the inner dictionary
|
| 34 |
+
inner_dict = {}
|
| 35 |
+
|
| 36 |
+
# Iterate over the rest of the elements in the row
|
| 37 |
+
for i, value in enumerate(row[1:], start=1):
|
| 38 |
+
# The corresponding column name is the key for the inner dictionary
|
| 39 |
+
inner_key = columns[i].strip()
|
| 40 |
+
# Add the key-value pair to the inner dictionary
|
| 41 |
+
inner_dict[inner_key] = value
|
| 42 |
+
|
| 43 |
+
# Add the inner dictionary to the result dictionary with the outer key
|
| 44 |
+
result[outer_key] = inner_dict
|
| 45 |
+
|
| 46 |
+
return result
|
| 47 |
+
|
| 48 |
+
def findTop3MoneyNeeds(proposition):
|
| 49 |
+
moneyNeeds, rows = fetch_db_rows_as_dicts('money_needs.sqlite', 'money_needs')
|
| 50 |
+
moneyNeedsDict = transform_to_dict_of_dicts(moneyNeeds, rows)
|
| 51 |
+
print(list(moneyNeedsDict.keys()))
|
| 52 |
+
needs = findTop3Needs(proposition, list(moneyNeedsDict.keys()))
|
| 53 |
+
needDictIndexes = []
|
| 54 |
+
for need in needs:
|
| 55 |
+
needDictIndexes.append(moneyNeedsDict[need])
|
| 56 |
+
|
| 57 |
+
print(needDictIndexes)
|
| 58 |
+
return needDictIndexes
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def findTop3Needs(proposition, moneyNeeds):
|
| 62 |
+
|
| 63 |
+
moneyNeedsString = concatenate_keys(moneyNeeds)
|
| 64 |
+
|
| 65 |
+
prompt = '''You have these listed needs of customers
|
| 66 |
+
{}
|
| 67 |
+
|
| 68 |
+
Now given a proposition
|
| 69 |
+
"{}"
|
| 70 |
+
|
| 71 |
+
Find the best 3 strings out of the list which matches this proposition. Return output strictly only in json under a list called matches
|
| 72 |
+
'''
|
| 73 |
+
|
| 74 |
+
moneyNeedsPrompt = prompt.format(moneyNeedsString, proposition)
|
| 75 |
+
response = model.generate_content([moneyNeedsPrompt])
|
| 76 |
+
output = response.text
|
| 77 |
+
output = output.replace('```json', '')
|
| 78 |
+
output = output.replace('```', '')
|
| 79 |
+
obj = load_json_from_string(output)
|
| 80 |
+
print(obj)
|
| 81 |
+
return obj['matches']
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
#findTop3MoneyNeeds('We have a product for family people giving them discounts and low interest loans for home appliances. They can pay us back in small instalments over the course of 4 years')
|