|
|
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() |
|
|
|
|
|
|
|
|
GROQ_API_KEY = os.getenv("GROQ_API_KEY") |
|
|
|
|
|
|
|
|
groq_client = Groq(api_key=GROQ_API_KEY) |
|
|
|
|
|
|
|
|
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." |
|
|
] |
|
|
|
|
|
|
|
|
embed_model = SentenceTransformer("all-MiniLM-L6-v2") |
|
|
doc_embeddings = embed_model.encode(docs) |
|
|
|
|
|
|
|
|
index = faiss.IndexFlatL2(doc_embeddings.shape[1]) |
|
|
index.add(np.array(doc_embeddings)) |
|
|
|
|
|
|
|
|
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", |
|
|
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)}") |
|
|
|