gk2410 commited on
Commit
b2ed227
·
verified ·
1 Parent(s): 3131056

update - v3

Browse files
Files changed (1) hide show
  1. app.py +19 -30
app.py CHANGED
@@ -1,27 +1,27 @@
1
- from chromadb.config import Settingsimport os
2
  import shutil
3
  import streamlit as st
4
  import chromadb
5
- from chromadb.config import Settings
6
  from sentence_transformers import SentenceTransformer
7
  from transformers import pipeline
8
  from weather import get_weather_summary
9
  from travel import get_travel_spots
10
  import spacy
11
 
12
- # Force Chroma to use a valid persist directory
13
- os.environ["PERSIST_DIRECTORY"] = "/tmp/chroma"
14
-
15
- # Clean up any stale DB state (optional but avoids schema errors)
16
- if os.path.exists("/tmp/chroma"):
17
- shutil.rmtree("/tmp/chroma")
18
-
19
- # Initialize Chroma with valid settings
20
- settings = Settings(
21
- chroma_db_impl="duckdb+parquet",
22
- persist_directory="/tmp/chroma"
 
23
  )
24
- client = chromadb.Client(settings)
25
  db = client.get_or_create_collection("disaster_news")
26
 
27
  # Load models
@@ -29,11 +29,11 @@ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
29
  qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
30
  nlp = spacy.load("en_core_web_sm")
31
 
32
- # Define keyword lists
33
  weather_keywords = ["weather", "forecast", "rain", "snow", "temperature", "wind", "climate", "humid", "cold", "hot"]
34
  travel_keywords = ["visit", "travel", "tourist", "see", "go", "spots", "places", "explore", "attractions"]
35
 
36
- # Extract location and user intent
37
  def extract_location_and_intent(query):
38
  doc = nlp(query)
39
  locations = [ent.text for ent in doc.ents if ent.label_ in ("GPE", "LOC")]
@@ -62,16 +62,13 @@ if st.button("Submit") and user_input:
62
  response_parts = []
63
 
64
  if is_weather and location:
65
- weather_summary = get_weather_summary(location)
66
- response_parts.append(weather_summary)
67
 
68
  if is_travel and location:
69
- travel_suggestions = get_travel_spots(location)
70
- response_parts.append(travel_suggestions)
71
 
72
  if not is_weather and not is_travel:
73
- rag_response = query_rag_system(user_input)
74
- response_parts.append(rag_response)
75
 
76
  if not response_parts:
77
  response_parts.append("❓ Couldn't understand your question or find relevant data.")
@@ -79,11 +76,3 @@ if st.button("Submit") and user_input:
79
  st.markdown("### 🔎 Response:")
80
  for part in response_parts:
81
  st.write(part)
82
-
83
-
84
- settings = Settings(
85
- chroma_db_impl="duckdb+parquet",
86
- persist_directory="/tmp/chroma" # must not be None
87
- )
88
-
89
- client = chromadb.Client(settings)
 
1
+ import os
2
  import shutil
3
  import streamlit as st
4
  import chromadb
5
+ from chromadb.config import Settings, DEFAULT_TENANT, DEFAULT_DATABASE
6
  from sentence_transformers import SentenceTransformer
7
  from transformers import pipeline
8
  from weather import get_weather_summary
9
  from travel import get_travel_spots
10
  import spacy
11
 
12
+ # Ensure a fresh Chroma DB directory (temporary path)
13
+ CHROMA_PATH = "/tmp/chroma"
14
+ if os.path.exists(CHROMA_PATH):
15
+ shutil.rmtree(CHROMA_PATH)
16
+ os.makedirs(CHROMA_PATH, exist_ok=True)
17
+
18
+ # Initialize Chroma using the new client API
19
+ client = chromadb.PersistentClient(
20
+ path=CHROMA_PATH,
21
+ settings=Settings(),
22
+ tenant=DEFAULT_TENANT,
23
+ database=DEFAULT_DATABASE,
24
  )
 
25
  db = client.get_or_create_collection("disaster_news")
26
 
27
  # Load models
 
29
  qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
30
  nlp = spacy.load("en_core_web_sm")
31
 
32
+ # Keyword lists
33
  weather_keywords = ["weather", "forecast", "rain", "snow", "temperature", "wind", "climate", "humid", "cold", "hot"]
34
  travel_keywords = ["visit", "travel", "tourist", "see", "go", "spots", "places", "explore", "attractions"]
35
 
36
+ # Extract location and intent
37
  def extract_location_and_intent(query):
38
  doc = nlp(query)
39
  locations = [ent.text for ent in doc.ents if ent.label_ in ("GPE", "LOC")]
 
62
  response_parts = []
63
 
64
  if is_weather and location:
65
+ response_parts.append(get_weather_summary(location))
 
66
 
67
  if is_travel and location:
68
+ response_parts.append(get_travel_spots(location))
 
69
 
70
  if not is_weather and not is_travel:
71
+ response_parts.append(query_rag_system(user_input))
 
72
 
73
  if not response_parts:
74
  response_parts.append("❓ Couldn't understand your question or find relevant data.")
 
76
  st.markdown("### 🔎 Response:")
77
  for part in response_parts:
78
  st.write(part)