Engineer786 commited on
Commit
5230b46
·
verified ·
1 Parent(s): b55d380

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -9,7 +9,10 @@ from groq import Groq
9
  # Fetch API key from environment variable
10
  API_KEY = os.environ.get('GroqApi')
11
 
12
- #API_KEY = os.environ.get("GROQ_API_KEY")
 
 
 
13
 
14
  # Function to scrape tariff data
15
  def scrape_tariff_data(url):
@@ -37,17 +40,25 @@ def create_faiss_index(chunks, model_name='all-MiniLM-L6-v2'):
37
  index.add(embeddings)
38
  return index, embeddings, model
39
 
40
- # Function to query the Groq API
41
- def query_llm(prompt):
 
 
 
 
 
 
 
42
  if not API_KEY:
43
  return "Error: GROQ_API_KEY is not set in environment variables."
44
 
45
  client = Groq(api_key=API_KEY)
 
46
  chat_completion = client.chat.completions.create(
47
  messages=[
48
  {
49
  "role": "user",
50
- "content": prompt,
51
  }
52
  ],
53
  model="llama3-8b-8192",
@@ -63,8 +74,9 @@ if st.button("Process Tariff Data"):
63
  with st.spinner("Extracting and processing data..."):
64
  try:
65
  text = scrape_tariff_data(url)
66
- chunks = chunk_text(text)
67
- index, embeddings, model = create_faiss_index(chunks)
 
68
  st.success("Data processed and indexed!")
69
  except Exception as e:
70
  st.error(f"Error processing data: {e}")
@@ -76,8 +88,16 @@ if st.button("Get Answer"):
76
  if prompt:
77
  with st.spinner("Fetching response..."):
78
  try:
79
- response = query_llm(prompt)
80
- st.write(response)
 
 
 
 
 
 
 
 
81
  except Exception as e:
82
  st.error(f"Error querying the model: {e}")
83
  else:
 
9
  # Fetch API key from environment variable
10
  API_KEY = os.environ.get('GroqApi')
11
 
12
+ # Initialize variables
13
+ INDEX = None
14
+ CHUNKS = None
15
+ MODEL = None
16
 
17
  # Function to scrape tariff data
18
  def scrape_tariff_data(url):
 
40
  index.add(embeddings)
41
  return index, embeddings, model
42
 
43
+ # Function to search FAISS for relevant chunks
44
+ def search_faiss(query, index, chunks, model, top_k=5):
45
+ query_embedding = model.encode([query])
46
+ distances, indices = index.search(query_embedding, top_k)
47
+ relevant_chunks = [chunks[i] for i in indices[0] if i < len(chunks)]
48
+ return relevant_chunks
49
+
50
+ # Function to query the Groq API with augmented query
51
+ def query_llm(prompt, context):
52
  if not API_KEY:
53
  return "Error: GROQ_API_KEY is not set in environment variables."
54
 
55
  client = Groq(api_key=API_KEY)
56
+ augmented_prompt = f"Based on the following data:\n\n{context}\n\nAnswer the question: {prompt}"
57
  chat_completion = client.chat.completions.create(
58
  messages=[
59
  {
60
  "role": "user",
61
+ "content": augmented_prompt,
62
  }
63
  ],
64
  model="llama3-8b-8192",
 
74
  with st.spinner("Extracting and processing data..."):
75
  try:
76
  text = scrape_tariff_data(url)
77
+ global CHUNKS, INDEX, MODEL
78
+ CHUNKS = chunk_text(text)
79
+ INDEX, embeddings, MODEL = create_faiss_index(CHUNKS)
80
  st.success("Data processed and indexed!")
81
  except Exception as e:
82
  st.error(f"Error processing data: {e}")
 
88
  if prompt:
89
  with st.spinner("Fetching response..."):
90
  try:
91
+ if not (INDEX and CHUNKS and MODEL):
92
+ st.error("Data has not been processed yet. Please process the data first.")
93
+ else:
94
+ # Retrieve relevant chunks
95
+ relevant_chunks = search_faiss(prompt, INDEX, CHUNKS, MODEL)
96
+ context = "\n".join(relevant_chunks)
97
+
98
+ # Query the LLM with context
99
+ response = query_llm(prompt, context)
100
+ st.write(response)
101
  except Exception as e:
102
  st.error(f"Error querying the model: {e}")
103
  else: