File size: 3,085 Bytes
01b406f
e880710
 
ef6e80f
01b406f
255977c
5bdc2f8
e880710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef6e80f
e880710
ef6e80f
 
 
e880710
255977c
 
 
 
 
 
 
 
e880710
 
ef6e80f
 
 
 
 
e880710
ef6e80f
 
 
 
 
 
e880710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255977c
e880710
255977c
e880710
 
255977c
e880710
 
 
255977c
e880710
 
255977c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import faiss
from sentence_transformers import SentenceTransformer
from groq import Groq

# Initialize Groq API
client = Groq(api_key="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")  # Ensure your API key is valid

# Initialize Sentence Transformer
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')

# FAISS Index
dimension = 384  # Embedding dimension of the model
index = faiss.IndexFlatL2(dimension)

# Function to chunk text
def chunk_text(text, max_length=500):
    words = text.split()
    chunks = []
    chunk = []
    for word in words:
        if len(" ".join(chunk)) + len(word) <= max_length:
            chunk.append(word)
        else:
            chunks.append(" ".join(chunk))
            chunk = [word]
    if chunk:
        chunks.append(" ".join(chunk))
    return chunks

# Function to embed text and add to FAISS index
def embed_and_store(chunks):
    embeddings = embedding_model.encode(chunks)
    index.add(embeddings)

# Query handling using Groq's streaming completions
def query_llm(prompt):
    # Create a completion request using the Groq model
    completion = client.chat.completions.create(
        model="deepseek-r1-distill-llama-70b",  # Use the provided Groq model
        messages=[
            {
                "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."
                )
            },
            {"role": "user", "content": prompt},
        ],
        temperature=0.6,
        max_completion_tokens=1024,
        top_p=0.95,
        stream=True,
        reasoning_format="raw"
    )
    
    # Stream and collect the response
    full_response = ""
    for chunk in completion:
        full_response += chunk.choices[0].delta.content or ""
    return full_response

# Streamlit App
st.title("AI Relationship Counsellor")

uploaded_file = st.file_uploader("Upload a text file of your WhatsApp chat", type=["txt"])

if uploaded_file:
    text = uploaded_file.read().decode("utf-8")
    st.write("Chat Extracted Successfully!")

    # Chunk and embed text
    chunks = chunk_text(text)
    embed_and_store(chunks)

    # Query Interface
    user_query = st.text_input("Ask a question about your relationship:")
    if user_query:
        # Embed query and search FAISS for the top 5 relevant chunks
        query_embedding = embedding_model.encode([user_query])
        distances, indices = index.search(query_embedding, k=5)
        relevant_chunks = [chunks[i] for i in indices[0]]

        # Combine chunks to form context
        context = " ".join(relevant_chunks)
        final_prompt = f"Context: {context}\n\nQuestion: {user_query}"

        # Get response from the Groq model
        response = query_llm(final_prompt)
        st.write("### AI Analysis")
        st.write(response)