Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_community.vectorstores import Weaviate
|
| 4 |
from langchain_community.llms import Groq
|
|
@@ -6,12 +7,21 @@ from langchain.chains import RetrievalQA
|
|
| 6 |
from langchain.prompts import PromptTemplate
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
import weaviate
|
| 9 |
-
import os
|
| 10 |
|
|
|
|
| 11 |
WEAVIATE_URL = os.environ["WEAVIATE_URL"]
|
| 12 |
WEAVIATE_KEY = os.environ["WEAVIATE_KEY"]
|
| 13 |
os.environ["GROQ_API_KEY"] = os.environ["GROQ_API_KEY"]
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@gr.cache
|
| 16 |
def get_rag_chain():
|
| 17 |
client = weaviate.Client(url=WEAVIATE_URL, auth_client_secret=weaviate.AuthApiKey(WEAVIATE_KEY))
|
|
@@ -30,28 +40,20 @@ def search(query):
|
|
| 30 |
qa = get_rag_chain()
|
| 31 |
result = qa.invoke({"query": query})
|
| 32 |
answer = result["result"]
|
| 33 |
-
|
| 34 |
sources = "\n\n".join([
|
| 35 |
f"**{doc.metadata['title']}**\n{doc.page_content[:300]}..."
|
| 36 |
for doc in result["source_documents"]
|
| 37 |
])
|
| 38 |
return answer, sources
|
| 39 |
except Exception as e:
|
| 40 |
-
return f"Error: {str(e)}", "
|
| 41 |
|
| 42 |
# UI
|
| 43 |
-
with gr.Blocks(
|
| 44 |
-
gr.Markdown("# ArXiv RAG Search
|
| 45 |
-
gr.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
btn
|
| 49 |
-
|
| 50 |
-
with gr.Row():
|
| 51 |
-
out1 = gr.Markdown(label="Answer")
|
| 52 |
-
out2 = gr.Markdown(label="Sources")
|
| 53 |
-
|
| 54 |
-
btn.click(search, inputs=txt, outputs=[out1, out2])
|
| 55 |
-
txt.submit(btn)
|
| 56 |
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
import gradio as gr
|
| 4 |
from langchain_community.vectorstores import Weaviate
|
| 5 |
from langchain_community.llms import Groq
|
|
|
|
| 7 |
from langchain.prompts import PromptTemplate
|
| 8 |
from sentence_transformers import SentenceTransformer
|
| 9 |
import weaviate
|
|
|
|
| 10 |
|
| 11 |
+
# SECRETS
|
| 12 |
WEAVIATE_URL = os.environ["WEAVIATE_URL"]
|
| 13 |
WEAVIATE_KEY = os.environ["WEAVIATE_KEY"]
|
| 14 |
os.environ["GROQ_API_KEY"] = os.environ["GROQ_API_KEY"]
|
| 15 |
|
| 16 |
+
# AUTO-INGEST ON START
|
| 17 |
+
def run_ingestion():
|
| 18 |
+
print("Running ingestion...")
|
| 19 |
+
subprocess.run(["python", "ingest.py"], check=True)
|
| 20 |
+
print("Ingestion complete!")
|
| 21 |
+
|
| 22 |
+
run_ingestion()
|
| 23 |
+
|
| 24 |
+
# RAG CHAIN
|
| 25 |
@gr.cache
|
| 26 |
def get_rag_chain():
|
| 27 |
client = weaviate.Client(url=WEAVIATE_URL, auth_client_secret=weaviate.AuthApiKey(WEAVIATE_KEY))
|
|
|
|
| 40 |
qa = get_rag_chain()
|
| 41 |
result = qa.invoke({"query": query})
|
| 42 |
answer = result["result"]
|
|
|
|
| 43 |
sources = "\n\n".join([
|
| 44 |
f"**{doc.metadata['title']}**\n{doc.page_content[:300]}..."
|
| 45 |
for doc in result["source_documents"]
|
| 46 |
])
|
| 47 |
return answer, sources
|
| 48 |
except Exception as e:
|
| 49 |
+
return f"Error: {str(e)}", ""
|
| 50 |
|
| 51 |
# UI
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("# ArXiv RAG Search")
|
| 54 |
+
txt = gr.Textbox(label="Ask", placeholder="What is attention?")
|
| 55 |
+
btn = gr.Button("Search")
|
| 56 |
+
out1 = gr.Markdown(); out2 = gr.Markdown()
|
| 57 |
+
btn.click(search, txt, [out1, out2])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
demo.launch()
|