Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,15 @@ import PyPDF2
|
|
| 3 |
import tiktoken
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
import requests
|
| 8 |
|
| 9 |
# Load embedding model
|
| 10 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 11 |
|
| 12 |
-
# GROQ API configuration
|
| 13 |
-
GROQ_API_KEY = "
|
| 14 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 15 |
LLAMA3_MODEL = "llama3-8b-8192"
|
| 16 |
|
|
@@ -42,6 +43,9 @@ def search_index(index, query, chunks, top_k=3):
|
|
| 42 |
|
| 43 |
# Generate answer using GROQ
|
| 44 |
def generate_answer(prompt):
|
|
|
|
|
|
|
|
|
|
| 45 |
headers = {
|
| 46 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 47 |
"Content-Type": "application/json"
|
|
@@ -81,9 +85,12 @@ def main():
|
|
| 81 |
top_chunks = search_index(st.session_state.index, query, st.session_state.chunks)
|
| 82 |
context = "\n\n".join(top_chunks)
|
| 83 |
prompt = f"Use the following context to answer the question:\n\n{context}\n\nQuestion: {query}"
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
main()
|
|
|
|
| 3 |
import tiktoken
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
+
import os
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
import requests
|
| 9 |
|
| 10 |
# Load embedding model
|
| 11 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
|
| 13 |
+
# GROQ API configuration (🔐 loaded securely from environment variable)
|
| 14 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 15 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 16 |
LLAMA3_MODEL = "llama3-8b-8192"
|
| 17 |
|
|
|
|
| 43 |
|
| 44 |
# Generate answer using GROQ
|
| 45 |
def generate_answer(prompt):
|
| 46 |
+
if not GROQ_API_KEY:
|
| 47 |
+
return "🚫 GROQ API key not found. Please set it in environment variables."
|
| 48 |
+
|
| 49 |
headers = {
|
| 50 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 51 |
"Content-Type": "application/json"
|
|
|
|
| 85 |
top_chunks = search_index(st.session_state.index, query, st.session_state.chunks)
|
| 86 |
context = "\n\n".join(top_chunks)
|
| 87 |
prompt = f"Use the following context to answer the question:\n\n{context}\n\nQuestion: {query}"
|
| 88 |
+
try:
|
| 89 |
+
answer = generate_answer(prompt)
|
| 90 |
+
st.markdown("### 🧠 Answer:")
|
| 91 |
+
st.write(answer)
|
| 92 |
+
except requests.exceptions.HTTPError as e:
|
| 93 |
+
st.error(f"❌ API Error: {e}")
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
main()
|