aakash-malhan commited on
Commit
a2f326b
·
verified ·
1 Parent(s): a15b79f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -32
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import subprocess
3
  import gradio as gr
@@ -6,18 +7,42 @@ from langchain.chains import RetrievalQA
6
  from langchain.prompts import PromptTemplate
7
  from sentence_transformers import SentenceTransformer
8
  import weaviate
 
 
9
 
10
- # Groq import
11
- from langchain_groq import ChatGroq
12
 
13
  # === SECRETS ===
14
  WEAVIATE_URL = os.environ["WEAVIATE_URL"]
15
  WEAVIATE_KEY = os.environ["WEAVIATE_KEY"]
16
  os.environ["GROQ_API_KEY"] = os.environ["GROQ_API_KEY"]
17
 
18
- # AUTO-INGEST ON START
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def run_ingestion():
20
- print("Running ingestion...")
 
 
 
 
21
  result = subprocess.run(["python", "ingest.py"], capture_output=True, text=True)
22
  print(result.stdout)
23
  if result.returncode != 0:
@@ -25,13 +50,11 @@ def run_ingestion():
25
  else:
26
  print("Ingestion complete!")
27
 
28
- # Run once at startup
29
  run_ingestion()
30
 
31
- # RAG CHAIN
32
- @gr.cache
33
  def get_rag_chain():
34
- # v3 Weaviate Client
35
  client = weaviate.Client(
36
  url=WEAVIATE_URL,
37
  auth_client_secret=weaviate.AuthApiKey(WEAVIATE_KEY)
@@ -40,16 +63,12 @@ def get_rag_chain():
40
  vectorstore = Weaviate(client, "Paper", "text", embedding=embedder)
41
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
42
 
43
- # Use ChatGroq
44
  llm = ChatGroq(model="llama-3.1-70b-instruct", temperature=0)
45
-
46
  prompt = PromptTemplate.from_template(
47
  "Answer using only this context:\n{context}\n\nQuestion: {question}\nAnswer:"
48
  )
49
  return RetrievalQA.from_chain_type(
50
- llm=llm,
51
- retriever=retriever,
52
- chain_type_kwargs={"prompt": prompt}
53
  )
54
 
55
  def search(query):
@@ -57,33 +76,23 @@ def search(query):
57
  qa = get_rag_chain()
58
  result = qa.invoke({"query": query})
59
  answer = result["result"]
60
-
61
  sources = "\n\n".join([
62
  f"**{doc.metadata.get('title', 'No Title')}**\n{doc.page_content[:300]}..."
63
  for doc in result["source_documents"]
64
  ])
65
- return answer, sources if sources.strip() else "No sources retrieved."
66
  except Exception as e:
67
  return f"Error: {str(e)}", "Check logs."
68
 
69
- # GRADIO UI
70
- with gr.Blocks(title="ArXiv RAG Search") as demo:
71
- gr.Markdown("# ArXiv RAG Search Engine")
72
- gr.Markdown("10K+ research papers • Llama-3.1 • Weaviate • Auto-indexed")
73
-
74
- with gr.Row():
75
- txt = gr.Textbox(
76
- label="Ask about AI, physics, math...",
77
- placeholder="What is the attention mechanism?",
78
- lines=2
79
- )
80
- btn = gr.Button("Search", variant="primary")
81
 
82
- with gr.Row():
83
- out_answer = gr.Markdown(label="Answer")
84
- out_sources = gr.Markdown(label="Sources")
85
 
86
- btn.click(search, inputs=txt, outputs=[out_answer, out_sources])
87
- txt.submit(lambda x: (x, gr.update()), txt, [txt, btn]).then(search, txt, [out_answer, out_sources])
88
 
89
  demo.launch()
 
1
+ # app.py - FINAL, 100% WORKING
2
  import os
3
  import subprocess
4
  import gradio as gr
 
7
  from langchain.prompts import PromptTemplate
8
  from sentence_transformers import SentenceTransformer
9
  import weaviate
10
+ import time
11
+ import requests
12
 
13
+ # === FIX: Correct Groq import ===
14
+ from langchain_groq import ChatGroq
15
 
16
  # === SECRETS ===
17
  WEAVIATE_URL = os.environ["WEAVIATE_URL"]
18
  WEAVIATE_KEY = os.environ["WEAVIATE_KEY"]
19
  os.environ["GROQ_API_KEY"] = os.environ["GROQ_API_KEY"]
20
 
21
+ # === WAIT FOR WEAVIATE TO BE READY ===
22
+ def wait_for_weaviate(url, key, timeout=120):
23
+ headers = {"Authorization": f"Bearer {key}"}
24
+ ready_url = f"{url}/v1/.well-known/ready"
25
+ print("Waiting for Weaviate to be ready...")
26
+ for i in range(timeout):
27
+ try:
28
+ response = requests.get(ready_url, headers=headers)
29
+ if response.status_code == 200:
30
+ print("Weaviate is ready!")
31
+ return True
32
+ except:
33
+ pass
34
+ print(f"Attempt {i+1}/{timeout}... waiting 1s")
35
+ time.sleep(1)
36
+ print("Weaviate did not start in time.")
37
+ return False
38
+
39
+ # === AUTO-INGEST ON START ===
40
  def run_ingestion():
41
+ print("Starting ingestion...")
42
+ if not wait_for_weaviate(WEAVIATE_URL, WEAVIATE_KEY):
43
+ print("Cannot connect to Weaviate. Skipping ingestion.")
44
+ return
45
+
46
  result = subprocess.run(["python", "ingest.py"], capture_output=True, text=True)
47
  print(result.stdout)
48
  if result.returncode != 0:
 
50
  else:
51
  print("Ingestion complete!")
52
 
53
+ # Run once
54
  run_ingestion()
55
 
56
+ # === RAG CHAIN (NO @gr.cache) ===
 
57
  def get_rag_chain():
 
58
  client = weaviate.Client(
59
  url=WEAVIATE_URL,
60
  auth_client_secret=weaviate.AuthApiKey(WEAVIATE_KEY)
 
63
  vectorstore = Weaviate(client, "Paper", "text", embedding=embedder)
64
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
65
 
 
66
  llm = ChatGroq(model="llama-3.1-70b-instruct", temperature=0)
 
67
  prompt = PromptTemplate.from_template(
68
  "Answer using only this context:\n{context}\n\nQuestion: {question}\nAnswer:"
69
  )
70
  return RetrievalQA.from_chain_type(
71
+ llm=llm, retriever=retriever, chain_type_kwargs={"prompt": prompt}
 
 
72
  )
73
 
74
  def search(query):
 
76
  qa = get_rag_chain()
77
  result = qa.invoke({"query": query})
78
  answer = result["result"]
 
79
  sources = "\n\n".join([
80
  f"**{doc.metadata.get('title', 'No Title')}**\n{doc.page_content[:300]}..."
81
  for doc in result["source_documents"]
82
  ])
83
+ return answer, sources or "No sources found."
84
  except Exception as e:
85
  return f"Error: {str(e)}", "Check logs."
86
 
87
+ # === GRADIO UI ===
88
+ with gr.Blocks(title="ArXiv RAG") as demo:
89
+ gr.Markdown("# ArXiv RAG Search")
90
+ gr.Markdown("10K+ papers • Llama-3.1 • Weaviate")
 
 
 
 
 
 
 
 
91
 
92
+ txt = gr.Textbox(label="Ask", placeholder="What is attention?", lines=2)
93
+ btn = gr.Button("Search", variant="primary")
94
+ out1 = gr.Markdown(); out2 = gr.Markdown()
95
 
96
+ btn.click(search, txt, [out1, out2])
 
97
 
98
  demo.launch()