Spaces:
Sleeping
Sleeping
update - v3
Browse files
app.py
CHANGED
|
@@ -1,27 +1,27 @@
|
|
| 1 |
-
|
| 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 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 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 |
-
#
|
| 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
|
| 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 |
-
|
| 66 |
-
response_parts.append(weather_summary)
|
| 67 |
|
| 68 |
if is_travel and location:
|
| 69 |
-
|
| 70 |
-
response_parts.append(travel_suggestions)
|
| 71 |
|
| 72 |
if not is_weather and not is_travel:
|
| 73 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|