Spaces:
Running
Running
Ashmi Banerjee
commited on
Commit
·
420fa8a
1
Parent(s):
b6dfdd7
updates with gemini
Browse files- .gitignore +184 -0
- build_rag.py +53 -0
- models/gemini.py +19 -0
- models/gemma.py +21 -0
- requirements.txt +7 -1
- setups/db_setup.py +27 -0
- setups/vertex_ai_setup.py +20 -0
.gitignore
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
.idea/
|
161 |
+
.ipynb_checkpoints
|
162 |
+
config
|
163 |
+
notebooks/.ipynb_checkpoints
|
164 |
+
.DS_Store
|
165 |
+
notebooks/.DS_Store
|
166 |
+
notebooks/.*
|
167 |
+
scripts/.*
|
168 |
+
data/embeddings/
|
169 |
+
review-analysis-palm/data/
|
170 |
+
gemma/data/
|
171 |
+
config/
|
172 |
+
gemma/flagged/
|
173 |
+
gemma/.ipynb_checkpoints/
|
174 |
+
gemma/.DS_Store
|
175 |
+
gemma/notebooks/.ipynb_checkpoints/
|
176 |
+
gemini/.ipynb_checkpoints/
|
177 |
+
.github/
|
178 |
+
gradio_cached_examples/
|
179 |
+
.idea/
|
180 |
+
__pycache__/
|
181 |
+
gradio_cached_examples/
|
182 |
+
models/__pycache__/
|
183 |
+
setups/__pycache__/
|
184 |
+
setups/gcp-default-creds.json
|
build_rag.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer
|
2 |
+
from setups.db_setup import get_mongo_client, get_mongo_url
|
3 |
+
|
4 |
+
|
5 |
+
def get_embedding(text: str) -> list[float]:
|
6 |
+
embedding_model = SentenceTransformer("thenlper/gte-large")
|
7 |
+
|
8 |
+
if not text.strip():
|
9 |
+
print("Attempted to get embedding for empty text.")
|
10 |
+
return []
|
11 |
+
|
12 |
+
embedding = embedding_model.encode(text)
|
13 |
+
|
14 |
+
return embedding.tolist()
|
15 |
+
|
16 |
+
|
17 |
+
def query_results(query, mongo_url):
|
18 |
+
mongo_client = get_mongo_client(mongo_url)
|
19 |
+
db = mongo_client["EU_Cities"]
|
20 |
+
|
21 |
+
query_embedding = get_embedding(query)
|
22 |
+
results = db.EU_cities_collection.aggregate([
|
23 |
+
{
|
24 |
+
"$vectorSearch": {
|
25 |
+
"index": "vector_index",
|
26 |
+
"path": "embedding",
|
27 |
+
"queryVector": query_embedding,
|
28 |
+
"numCandidates": 150,
|
29 |
+
"limit": 5
|
30 |
+
}
|
31 |
+
}
|
32 |
+
])
|
33 |
+
return results
|
34 |
+
|
35 |
+
|
36 |
+
def get_search_result(query, mongo_url):
|
37 |
+
get_knowledge = query_results(query, mongo_url)
|
38 |
+
print(get_knowledge)
|
39 |
+
|
40 |
+
search_result = ""
|
41 |
+
for result in get_knowledge:
|
42 |
+
search_result += f"City: {result.get('city', 'N/A')}, Abstract: {result.get('combined', 'N/A')}\n"
|
43 |
+
|
44 |
+
return search_result
|
45 |
+
|
46 |
+
|
47 |
+
def get_context(query: str) -> str:
|
48 |
+
mongo_url = get_mongo_url()
|
49 |
+
source_information = get_search_result(query, mongo_url)
|
50 |
+
combined_information = (
|
51 |
+
f"Query: {query}\nContinue to answer the query by using the Search Results:\n{source_information}."
|
52 |
+
)
|
53 |
+
return combined_information
|
models/gemini.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
import sys
|
3 |
+
|
4 |
+
sys.path.append("../")
|
5 |
+
from setups.vertex_ai_setup import initialize_vertexai_params
|
6 |
+
from vertexai.preview.generative_models import GenerativeModel
|
7 |
+
|
8 |
+
|
9 |
+
def get_gemini_response(prompt_text, model, parameters: Optional = None) -> str:
|
10 |
+
initialize_vertexai_params()
|
11 |
+
if model is None or parameters is None:
|
12 |
+
model = "gemini-1.0-pro"
|
13 |
+
parameters = {
|
14 |
+
"max_output_tokens": 1024,
|
15 |
+
"temperature": 0.2
|
16 |
+
}
|
17 |
+
model = GenerativeModel.from_pretrained(model, **parameters)
|
18 |
+
model_response = model.generate_content(prompt_text)
|
19 |
+
return model_response
|
models/gemma.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
|
9 |
+
def gemma_predict(combined_information, model_name):
|
10 |
+
HF_token = os.environ["HF_TOKEN"]
|
11 |
+
client = InferenceClient(model_name, token=HF_token)
|
12 |
+
stream = client.text_generation(prompt=combined_information, details=True, stream=True, max_new_tokens=2048,
|
13 |
+
return_full_text=False)
|
14 |
+
output = ""
|
15 |
+
|
16 |
+
for response in stream:
|
17 |
+
output += response.token.text
|
18 |
+
|
19 |
+
if "<eos>" in output:
|
20 |
+
output = output.split("<eos>")[0]
|
21 |
+
return output
|
requirements.txt
CHANGED
@@ -1,4 +1,10 @@
|
|
1 |
sentence-transformers==2.5.1
|
2 |
gradio==3.45.0
|
3 |
gradio_client==0.5.2
|
4 |
-
pymongo==4.6.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
sentence-transformers==2.5.1
|
2 |
gradio==3.45.0
|
3 |
gradio_client==0.5.2
|
4 |
+
pymongo==4.6.2
|
5 |
+
python-dotenv
|
6 |
+
google-cloud-aiplatform
|
7 |
+
google-cloud
|
8 |
+
vertexai==1.43.0
|
9 |
+
huggingface_hub==0.22.0
|
10 |
+
certifi==2021.5.30
|
setups/db_setup.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import certifi
|
2 |
+
import pymongo
|
3 |
+
import os
|
4 |
+
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
|
10 |
+
def get_mongo_client(mongo_url):
|
11 |
+
"""Establish connection to the MongoDB."""
|
12 |
+
if not mongo_url:
|
13 |
+
print("MONGO_URI not set in environment variables")
|
14 |
+
try:
|
15 |
+
client = pymongo.MongoClient(mongo_url, tlsCAFile=certifi.where())
|
16 |
+
print("Connection to MongoDB successful")
|
17 |
+
return client
|
18 |
+
except pymongo.errors.ConnectionFailure as e:
|
19 |
+
print(f"Connection failed: {e}")
|
20 |
+
return None
|
21 |
+
|
22 |
+
|
23 |
+
def get_mongo_url():
|
24 |
+
username = os.environ["MONGO_USERNAME"]
|
25 |
+
password = os.environ["MONGO_PW"]
|
26 |
+
mongo_url = f"mongodb+srv://{username}:{password}@cluster0.62unmco.mongodb.net/"
|
27 |
+
return mongo_url
|
setups/vertex_ai_setup.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from google.oauth2 import service_account
|
4 |
+
import vertexai
|
5 |
+
import os
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# TODO: fix it in spaces
|
10 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "gcp-default-creds.json"
|
11 |
+
GOOGLE_APPLICATION_CREDENTIALS = os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
|
12 |
+
VERTEXAI_PROJECT = os.environ["VERTEXAI_PROJECT"]
|
13 |
+
|
14 |
+
|
15 |
+
def initialize_vertexai_params(location: Optional[str] = "us-central1"):
|
16 |
+
service_account.Credentials.from_service_account_file(
|
17 |
+
filename=GOOGLE_APPLICATION_CREDENTIALS,
|
18 |
+
scopes=["https://www.googleapis.com/auth/cloud-platform"],
|
19 |
+
)
|
20 |
+
vertexai.init(project=VERTEXAI_PROJECT, location=location)
|