File size: 1,861 Bytes
1a7b2d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from groq import Groq
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import os
from dotenv import load_dotenv

load_dotenv()

# Load API key from .env or Hugging Face secret
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

# Initialize Groq client
groq_client = Groq(api_key=GROQ_API_KEY)

# Sample knowledge base
docs = [
    "Generative Engine Optimization improves AI answers for SEO.",
    "RAG combines retrieval with generation for accurate responses.",
    "Groq provides ultra-fast inference for LLMs.",
    "Streamlit is great for building quick ML apps.",
    "Hugging Face offers powerful transformer models and APIs."
]

# Load embedding model
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
doc_embeddings = embed_model.encode(docs)

# Create FAISS index
index = faiss.IndexFlatL2(doc_embeddings.shape[1])
index.add(np.array(doc_embeddings))

# Streamlit UI
st.set_page_config(page_title="GEO Optimizer MVP", layout="centered")
st.title("🔍 GEO Optimization Assistant")

query = st.text_input("Ask a question or enter a topic:")
if st.button("Generate Answer") and query:
    query_embedding = embed_model.encode([query])
    _, I = index.search(np.array(query_embedding), k=2)

    context = "\n".join([docs[i] for i in I[0]])

    prompt = f"""You are a helpful assistant. Use the following context to answer the question.
    
    Context:
    {context}
    
    Question: {query}
    
    Answer:"""

    try:
        response = groq_client.chat.completions.create(
            model="llama3-8b-8192",  # or whatever is available
            messages=[{"role": "user", "content": prompt}]
        )
        answer = response.choices[0].message.content
        st.markdown("### ✅ Answer")
        st.success(answer)
    except Exception as e:
        st.error(f"Error: {str(e)}")