MBilal-72 commited on
Commit
1a7b2d4
·
verified ·
1 Parent(s): 245184d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from groq import Groq
3
+ from sentence_transformers import SentenceTransformer
4
+ import faiss
5
+ import numpy as np
6
+ import os
7
+ from dotenv import load_dotenv
8
+
9
+ load_dotenv()
10
+
11
+ # Load API key from .env or Hugging Face secret
12
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
13
+
14
+ # Initialize Groq client
15
+ groq_client = Groq(api_key=GROQ_API_KEY)
16
+
17
+ # Sample knowledge base
18
+ docs = [
19
+ "Generative Engine Optimization improves AI answers for SEO.",
20
+ "RAG combines retrieval with generation for accurate responses.",
21
+ "Groq provides ultra-fast inference for LLMs.",
22
+ "Streamlit is great for building quick ML apps.",
23
+ "Hugging Face offers powerful transformer models and APIs."
24
+ ]
25
+
26
+ # Load embedding model
27
+ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
28
+ doc_embeddings = embed_model.encode(docs)
29
+
30
+ # Create FAISS index
31
+ index = faiss.IndexFlatL2(doc_embeddings.shape[1])
32
+ index.add(np.array(doc_embeddings))
33
+
34
+ # Streamlit UI
35
+ st.set_page_config(page_title="GEO Optimizer MVP", layout="centered")
36
+ st.title("🔍 GEO Optimization Assistant")
37
+
38
+ query = st.text_input("Ask a question or enter a topic:")
39
+ if st.button("Generate Answer") and query:
40
+ query_embedding = embed_model.encode([query])
41
+ _, I = index.search(np.array(query_embedding), k=2)
42
+
43
+ context = "\n".join([docs[i] for i in I[0]])
44
+
45
+ prompt = f"""You are a helpful assistant. Use the following context to answer the question.
46
+
47
+ Context:
48
+ {context}
49
+
50
+ Question: {query}
51
+
52
+ Answer:"""
53
+
54
+ try:
55
+ response = groq_client.chat.completions.create(
56
+ model="llama3-8b-8192", # or whatever is available
57
+ messages=[{"role": "user", "content": prompt}]
58
+ )
59
+ answer = response.choices[0].message.content
60
+ st.markdown("### ✅ Answer")
61
+ st.success(answer)
62
+ except Exception as e:
63
+ st.error(f"Error: {str(e)}")