Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,79 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import faiss
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
from openai import OpenAI
|
| 5 |
|
| 6 |
+
# Initialize OpenAI API
|
| 7 |
+
api_key = "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"
|
| 8 |
+
base_url = "https://api.aimlapi.com/v1"
|
| 9 |
+
api = OpenAI(api_key=api_key, base_url=base_url)
|
| 10 |
+
|
| 11 |
+
# Initialize Sentence Transformer
|
| 12 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 13 |
+
|
| 14 |
+
# FAISS Index
|
| 15 |
+
dimension = 384 # Embedding dimension of the model
|
| 16 |
+
index = faiss.IndexFlatL2(dimension)
|
| 17 |
+
|
| 18 |
+
# Function to chunk text
|
| 19 |
+
def chunk_text(text, max_length=500):
|
| 20 |
+
words = text.split()
|
| 21 |
+
chunks = []
|
| 22 |
+
chunk = []
|
| 23 |
+
for word in words:
|
| 24 |
+
if len(" ".join(chunk)) + len(word) <= max_length:
|
| 25 |
+
chunk.append(word)
|
| 26 |
+
else:
|
| 27 |
+
chunks.append(" ".join(chunk))
|
| 28 |
+
chunk = [word]
|
| 29 |
+
if chunk:
|
| 30 |
+
chunks.append(" ".join(chunk))
|
| 31 |
+
return chunks
|
| 32 |
+
|
| 33 |
+
# Function to embed text and add to FAISS index
|
| 34 |
+
def embed_and_store(chunks):
|
| 35 |
+
embeddings = embedding_model.encode(chunks)
|
| 36 |
+
index.add(embeddings)
|
| 37 |
+
|
| 38 |
+
# Query handling
|
| 39 |
+
def query_llm(prompt):
|
| 40 |
+
completion = api.chat.completions.create(
|
| 41 |
+
model="deepseek-ai/deepseek-llm-67b-chat",
|
| 42 |
+
messages=[
|
| 43 |
+
{"role": "system", "content": "You are a relationship counselor. Analyze the given WhatsApp conversation and provide insights on potential red flags, toxicity, and room for improvement in behavior. Every response must start by rating the overall chat toxicity out of 10."},
|
| 44 |
+
{"role": "user", "content": prompt},
|
| 45 |
+
],
|
| 46 |
+
temperature=0.7,
|
| 47 |
+
max_tokens=350,
|
| 48 |
+
)
|
| 49 |
+
return completion.choices[0].message.content
|
| 50 |
+
|
| 51 |
+
# Streamlit App
|
| 52 |
+
st.title("AI Relationship Counsellor")
|
| 53 |
+
|
| 54 |
+
uploaded_file = st.file_uploader("Upload a text file of your WhatsApp chat", type=["txt"])
|
| 55 |
+
|
| 56 |
+
if uploaded_file:
|
| 57 |
+
text = uploaded_file.read().decode("utf-8")
|
| 58 |
+
st.write("Chat Extracted Successfully!")
|
| 59 |
+
|
| 60 |
+
# Chunk and embed text
|
| 61 |
+
chunks = chunk_text(text)
|
| 62 |
+
embed_and_store(chunks)
|
| 63 |
+
|
| 64 |
+
# Query Interface
|
| 65 |
+
user_query = st.text_input("Ask a question about your relationship:")
|
| 66 |
+
if user_query:
|
| 67 |
+
# Embed query and search FAISS
|
| 68 |
+
query_embedding = embedding_model.encode([user_query])
|
| 69 |
+
distances, indices = index.search(query_embedding, k=5) # Top 5 results
|
| 70 |
+
relevant_chunks = [chunks[i] for i in indices[0]]
|
| 71 |
+
|
| 72 |
+
# Combine chunks for context
|
| 73 |
+
context = " ".join(relevant_chunks)
|
| 74 |
+
final_prompt = f"Context: {context}\n\nQuestion: {user_query}"
|
| 75 |
+
|
| 76 |
+
# Get response from AI model
|
| 77 |
+
response = query_llm(final_prompt)
|
| 78 |
+
st.write("### AI Analysis")
|
| 79 |
+
st.write(response)
|