Spaces:
Sleeping
Sleeping
import os; | |
import cohere; | |
import chromadb; | |
import numpy as np; | |
import gradio as gr; | |
from dotenv import load_dotenv; | |
from chromadb import PersistentClient | |
from chromadb.config import Settings, DEFAULT_TENANT, DEFAULT_DATABASE | |
load_dotenv() | |
CO_API_KEY = os.getenv( "COHERE_API_KEY" ); | |
co = cohere.Client( CO_API_KEY ); | |
EMBED_MODEL = "embed-v4.0" | |
CHAT_MODEL = "command-r-08-2024" | |
PERSIST_DIR = "/mnt/data/chromadb_storage" | |
os.makedirs(PERSIST_DIR, exist_ok=True) | |
#client = chromadb.Client( Settings( chroma_db_impl="duckdb+parquet", persist_directory=PERSIST_DIR ) ); | |
client = PersistentClient( | |
path=PERSIST_DIR, # where to store DuckDB+Parquet files | |
settings=Settings(), # default settings | |
tenant=DEFAULT_TENANT, # usually "default_tenant" | |
database=DEFAULT_DATABASE # usually "default_database" | |
); | |
collection = client.get_or_create_collection( name="brampton" ); | |
if collection.count() == 0: | |
brampton_docs = [ | |
# Shopping Centres | |
"Bramalea City Centre is one of Canada’s largest malls, with 1.5 million sq ft of retail space and over 300 stores.", | |
"Trinity Common Mall is a large outdoor shopping centre spanning over 850,000 sq ft with 69 retail stores, services, and restaurants at Hwy 410 & Bovaird Dr.", | |
"Shoppers World Brampton is a community shopping destination featuring over 180 shops and services at 499 Main St S, Suite 56, L6Y 1N7.", | |
"Brampton Mall houses 38 brand‑name stores at 152–160 Main St S, L6W 2C9, with services, restaurants, and customer amenities.", | |
# Entertainment & Recreation | |
"Toprock Climbing is Brampton’s first rock‑climbing gym (opened 2017), a community hub to meet new friends, challenge yourself, and stay fit.", | |
"Glow Zone 360 offers glow‑in‑the‑dark mini‑golf, laser tag, and arcade games at 9446 McLaughlin Rd N, Unit 7.", | |
"The CAA Centre is Brampton’s indoor sports and entertainment complex, hosting hockey games, trade shows, and community events at 7575 Kennedy Rd.", | |
# Parks & Outdoors | |
"Gage Park is an urban park at 45 Main St S offering strolling paths, picnic areas, flower gardens, free evening concerts, and a seasonal ice rink.", | |
"Professor’s Lake Recreation Centre features a 65‑acre lake with sandy beach, paddleboat/canoe/kayak rentals, walking trails, and fishing spots.", | |
"Eldorado Park is a scenic riverside park on the Credit River with picnic areas, walking trails, and a small lake for outdoor recreation.", | |
"Chinguacousy Park features a water park, splash pads, mini golf, ski hills, and biking trails at 9050 Bramalea Rd.", | |
"Heart Lake Conservation Park offers hiking, fishing, treetop trek canopy tours, and skiing at 11073 Heart Lake Rd.", | |
"Loafer’s Lake Recreation Centre is a community facility with ice rinks, outdoor pool, basketball courts, and scenic walking paths.", | |
"Claireville Conservation Area spans over 2,000 acres of wildlife habitat along the Humber River, with trails, picnic areas, and off‑road biking.", | |
# Culture & History | |
"Peel Art Gallery, Museum and Archives (PAMA) is a museum, art gallery, and archives for the Peel Region located at 9 Wellington St E in Brampton :contentReference[oaicite:3]{index=3}.", | |
"Rose Theatre Brampton is a municipally‑owned performing arts centre with an 868‑seat main hall and a smaller studio venue at 1 Theatre Ln :contentReference[oaicite:4]{index=4}.", | |
"Historic Bovaird House is a restored 1860s heritage farmhouse offering guided tours and seasonal events at 10 Chinguacousy Rd.", | |
"Historic Churchville Village is a preserved 19th‑century rural settlement, featuring St. James Anglican Church and original pioneer buildings.", | |
# Transit & Infrastructure | |
"Brampton GO Station connects downtown Brampton to Toronto via the GO Transit rail network at 27 Railway St.", | |
"Brampton Gateway Terminal is a major Brampton Transit bus hub offering city‑wide connections at Steeles Ave E & Main St.", | |
# Education & Health | |
"University of Guelph‑Humber is a joint campus offering undergraduate degrees in business, health, and arts at 207 Humber College Blvd.", | |
"Sheridan College Davis Campus offers programs in applied health, community studies, and media at 7899 McLaughlin Rd S.", | |
"Peel Memorial Centre for Integrated Health and Wellness provides hospital and outpatient services at 50 Queen St E.", | |
# Events & Festivals | |
"Carabram is an annual multicultural festival every July at Garden Square, showcasing pavilions, food, and performances from around the world.", | |
"The Brampton Santa Claus Parade, held every November along Queen St, features colourful floats, marching bands, and a visit from Santa.", | |
# Neighbourhoods & Demographics | |
"Springdale is a northern suburban district nicknamed “Singhdale” for its large Sikh Canadian community, bounded by Bovaird Dr, Heart Lake Rd, and Airport Rd :contentReference[oaicite:5]{index=5}.", | |
# General City Facts | |
"Official Name: City of Brampton.", | |
"Settlement Type: lower-tier city.", | |
"Nickname: Flower City (formerly 'Flower Town of Canada').", | |
"Part of the Greater Toronto Area.", | |
"Regional seat of the Regional Municipality of Peel.", | |
"Incorporated as a village in 1853.", | |
"Incorporated as a town in 1873.", | |
"Incorporated as a city in 1974.", | |
"Major highways: Highway 410 and Highway 407 ETR.", | |
# Geography & Climate | |
"Borders Vaughan to the east.", | |
"Borders Halton Hills to the west.", | |
"Borders Caledon to the north.", | |
"Borders Mississauga to the south.", | |
"Borders Etobicoke (Toronto) to the southeast.", | |
"Climate: Humid continental (Köppen Dfa).", | |
# Sister Cities | |
"Miami Beach, Florida.", | |
"Plano, Texas.", | |
"Ribeira Grande, Azores, Portugal.", | |
"Xuzhou, Jiangsu, China.", | |
"Brampton, Cumbria, England.", | |
"Marikina, Philippines.", | |
"Gapyeong, South Korea.", | |
# Neighbourhoods | |
"Alloa: rural former community in northwest Brampton.", | |
"Bramalea: Canada’s first planned satellite town.", | |
"Castlemore: suburban area with luxury homes in West Humber River valley.", | |
"Churchville: preserved 19th-century village on the Credit River.", | |
"Claireville: neighbourhood around Claireville Conservation Area.", | |
"Mount Pleasant: fast‑growing community since 2006 GO station.", | |
"Snelgrove: straddles Mayfield Road between Brampton and Caledon.", | |
"Springdale: 4,000‑acre northern district with large Sikh Canadian community.", | |
"Tullamore: small residential neighbourhood in Brampton.", | |
# History Highlights | |
"Originally inhabited by the Mississaugas of the Credit First Nation.", | |
"Treaty 19 (Ajetance Purchase) signed October 1818.", | |
"Village incorporation: 1853.", | |
"Town incorporation: 1873.", | |
"City status granted: 1974.", | |
"Named after Brampton, Cumberland (England).", | |
"Dale’s Nursery: once North America’s largest cut‑flower business.", | |
"Historic Bovaird House (1860s farmhouse) offers tours.", | |
"Historic Churchville Village preserves pioneer buildings.", | |
"Rose Theatre opened in September 2006.", | |
# Landmarks & Culture | |
"Peel Art Gallery, Museum and Archives (PAMA) at 9 Wellington St E.", | |
"Rose Theatre Brampton: 868‑seat main hall + studio theatre.", | |
"Heritage Theatre (former Capitol Theatre) preceded Rose Theatre.", | |
"Brampton City Hall at 2 Wellington St W; nine‑storey tower added 2014.", | |
"Garden Square: public plaza for concerts and events.", | |
"A. Grenville & W. Davis Courthouse at Hurontario & County Court St.", | |
# Shopping Centres | |
"Bramalea City Centre: 1.5 million sq ft, 300+ stores.", | |
"Trinity Common Mall: 850,000 sq ft open‑air centre, 69 retailers.", | |
"Shoppers World Brampton: 180+ shops at 499 Main St S.", | |
"Brampton Mall: 38 brand‑name stores at 152–160 Main St S.", | |
# Markets & Festivals | |
"Downtown Brampton Farmers’ Market at 30 Church St E (since 1859).", | |
"Carabram: annual multicultural festival every July.", | |
# Parks & Recreation | |
"Gage Park: oldest municipal park (est. 1903) with gardens, concerts.", | |
"Professor’s Lake Recreation Centre: 65‑acre lake, beach, rentals.", | |
"Eldorado Park: riverside park with playgrounds and pool on Credit River.", | |
"Chinguacousy Park: 100 acre green space with zoo, mini‑golf, ski hill.", | |
"Heart Lake Conservation Park: hiking, fishing, canopy tours.", | |
"Loafer’s Lake Recreation Centre: rinks, pool, courts, walking trails.", | |
"Claireville Conservation Area: 2,000 acre wildlife habitat.", | |
# Transit & Infrastructure | |
"Brampton GO Station connects via GO Transit rail (27 Railway St).", | |
"Mount Pleasant GO Station opened 2006 on Bradford line.", | |
"Lisgar GO Station serves western Brampton on Barrie line.", | |
"Bramalea GO Station hub at Fennell Blvd.", | |
"Brampton Gateway Terminal: central Brampton Transit bus hub.", | |
# Sports Teams | |
"Brampton Battalion: OHL team (1998–2013).", | |
"Brampton Beast: ECHL hockey (2013–2018).", | |
"Bramalea Blues: junior A ice hockey (OPJHL).", | |
"Bramalea Satellites: junior B hockey team.", | |
"Brampton City United FC: semi‑pro soccer club (founded 2007).", | |
"Brampton Honey Badgers: CEBL basketball champions (2022).", | |
"Brampton Excelsiors (MSL): Senior A box lacrosse (est. 1883).", | |
"Brampton Excelsiors Jr. A: OLA Junior A lacrosse league.", | |
"Brampton Excelsiors Jr. B: OJBLL box lacrosse expansion team (2012).", | |
# Education & Health | |
"University of Guelph–Humber: joint undergraduate campus in Brampton.", | |
"Sheridan College Davis Campus: applied health and media programs.", | |
"Peel Memorial Centre for Integrated Health and Wellness at 50 Queen St E.", | |
"William Osler Health System operates Brampton Civic Hospital on Bovaird Dr." | |
]; | |
doc_embeddings = co.embed( | |
model=EMBED_MODEL, | |
texts=brampton_docs | |
).embeddings; | |
brampton_vector = np.mean( doc_embeddings, axis=0 ); | |
collection.add( | |
ids=[f"doc_{i}" for i in range( len( brampton_docs ) )], | |
documents=brampton_docs, | |
embeddings=doc_embeddings, | |
metadatas=[{"source": f"doc_{i}"} for i in range( len( brampton_docs ) )] | |
); | |
#client.persist(); | |
SYSTEM_INSTR = ( | |
"You are a Brampton-focused assistant. " | |
"Only discuss topics related to Brampton. " | |
"If the user asks about anything else, politely redirect them back to Brampton topics." | |
); | |
def generate_response( prompt: str, max_tokens: int = 200 ) -> str: | |
resp = co.generate( | |
model=CHAT_MODEL, | |
prompt=prompt, | |
max_tokens=max_tokens, | |
return_likelihoods="NONE" | |
); | |
return resp.generations[0].text; | |
def brampton_response( query: str, k: int = 5, steering_scale: float = 0.5, sim_threshold: float = 0.3 ) -> str: | |
q_emb = np.array( co.embed(model=EMBED_MODEL, texts=[query]).embeddings[0] ); | |
sim = np.dot( q_emb, brampton_vector ) / ( np.linalg.norm( q_emb ) * np.linalg.norm( brampton_vector ) ); | |
if sim < sim_threshold: | |
return "I’m here to discuss Brampton, let's keep our conversation focused on this city!"; | |
steered_emb = q_emb + steering_scale * brampton_vector; | |
results = collection.query( query_embeddings=[steered_emb.tolist()], n_results=k ); | |
context = "\n".join( results["documents"][0] ); | |
prompt = ( | |
f"{SYSTEM_INSTR}\n" | |
f"Context:\n{context}\n\n" | |
f"User: {query}\n" | |
f"Assistant:" | |
); | |
return generate_response( prompt ); | |
def respond(message, chat_history): | |
bot_reply = brampton_response( message ); | |
chat_history = chat_history + [( message, bot_reply )]; | |
return "", chat_history; | |
with gr.Blocks() as demo: | |
gr.Markdown( "# Brampton Model" ); | |
chatbot = gr.Chatbot(); | |
user_input = gr.Textbox( show_label=False, placeholder="Ask me about anything" ); | |
user_input.submit( fn=respond, inputs=[user_input, chatbot], outputs=[user_input, chatbot] ); | |
gr.Markdown( "Powered by Cohere command-r, embed-v4.0." ); | |
# share=True creates a public URL when not on HF Spaces | |
if __name__ == "__main__": | |
demo.launch( share=True ); | |