Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import spaces
|
| 2 |
|
| 3 |
import os
|
| 4 |
import subprocess
|
|
@@ -10,7 +10,6 @@ from llama_index.vector_stores.qdrant import QdrantVectorStore
|
|
| 10 |
from qdrant_client import QdrantClient
|
| 11 |
from qdrant_client.models import Distance, VectorParams
|
| 12 |
|
| 13 |
-
# 1. Initialize Global Settings (forcing embeddings to CPU)
|
| 14 |
Settings.llm = Groq(model="llama3-8b-8192", api_key=os.environ.get("GROQ_API_KEY"))
|
| 15 |
Settings.embed_model = HuggingFaceEmbedding(
|
| 16 |
model_name="BAAI/bge-large-en-v1.5",
|
|
@@ -19,7 +18,6 @@ Settings.embed_model = HuggingFaceEmbedding(
|
|
| 19 |
Settings.chunk_size = 512
|
| 20 |
Settings.chunk_overlap = 50
|
| 21 |
|
| 22 |
-
# 2. Setup In-Memory Qdrant and Index
|
| 23 |
client = QdrantClient(location=":memory:")
|
| 24 |
client.create_collection(
|
| 25 |
collection_name="tech_docs",
|
|
@@ -28,7 +26,6 @@ client.create_collection(
|
|
| 28 |
vector_store = QdrantVectorStore(client=client, collection_name="tech_docs")
|
| 29 |
index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
| 30 |
|
| 31 |
-
# 3. Ingest Default GitHub Repo on Startup (FastAPI Docs)
|
| 32 |
TARGET_REPO = "https://github.com/tiangolo/fastapi.git"
|
| 33 |
clone_path = "/tmp/fastapi_docs"
|
| 34 |
|
|
@@ -43,21 +40,18 @@ try:
|
|
| 43 |
required_exts=[".md", ".mdx"],
|
| 44 |
recursive=True
|
| 45 |
).load_data()
|
| 46 |
-
|
| 47 |
for doc in documents[:200]:
|
| 48 |
index.insert(doc)
|
| 49 |
print("GitHub repository indexed successfully!")
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error indexing GitHub repo: {e}")
|
| 52 |
|
| 53 |
-
# Create global query engine
|
| 54 |
query_engine = index.as_query_engine(streaming=False)
|
| 55 |
|
| 56 |
-
# 4. Define Response Function for Gradio Chat Interface
|
| 57 |
def chat_with_docs(message, history, file_obj):
|
| 58 |
global index, query_engine
|
| 59 |
|
| 60 |
-
# If user uploaded a new PDF, ingest it dynamically into the active session
|
| 61 |
if file_obj is not None:
|
| 62 |
try:
|
| 63 |
pdf_docs = SimpleDirectoryReader(input_files=[file_obj.name]).load_data()
|
|
@@ -67,11 +61,9 @@ def chat_with_docs(message, history, file_obj):
|
|
| 67 |
except Exception as e:
|
| 68 |
return f"Error processing uploaded PDF: {str(e)}"
|
| 69 |
|
| 70 |
-
# Query the RAG engine using Groq acceleration
|
| 71 |
response = query_engine.query(message)
|
| 72 |
return str(response)
|
| 73 |
|
| 74 |
-
# 5. Build Gradio Chat UI Layout
|
| 75 |
demo = gr.ChatInterface(
|
| 76 |
fn=chat_with_docs,
|
| 77 |
title="📚 TechDocs-Knowledge-Engine",
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
|
| 3 |
import os
|
| 4 |
import subprocess
|
|
|
|
| 10 |
from qdrant_client import QdrantClient
|
| 11 |
from qdrant_client.models import Distance, VectorParams
|
| 12 |
|
|
|
|
| 13 |
Settings.llm = Groq(model="llama3-8b-8192", api_key=os.environ.get("GROQ_API_KEY"))
|
| 14 |
Settings.embed_model = HuggingFaceEmbedding(
|
| 15 |
model_name="BAAI/bge-large-en-v1.5",
|
|
|
|
| 18 |
Settings.chunk_size = 512
|
| 19 |
Settings.chunk_overlap = 50
|
| 20 |
|
|
|
|
| 21 |
client = QdrantClient(location=":memory:")
|
| 22 |
client.create_collection(
|
| 23 |
collection_name="tech_docs",
|
|
|
|
| 26 |
vector_store = QdrantVectorStore(client=client, collection_name="tech_docs")
|
| 27 |
index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
| 28 |
|
|
|
|
| 29 |
TARGET_REPO = "https://github.com/tiangolo/fastapi.git"
|
| 30 |
clone_path = "/tmp/fastapi_docs"
|
| 31 |
|
|
|
|
| 40 |
required_exts=[".md", ".mdx"],
|
| 41 |
recursive=True
|
| 42 |
).load_data()
|
| 43 |
+
|
| 44 |
for doc in documents[:200]:
|
| 45 |
index.insert(doc)
|
| 46 |
print("GitHub repository indexed successfully!")
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Error indexing GitHub repo: {e}")
|
| 49 |
|
|
|
|
| 50 |
query_engine = index.as_query_engine(streaming=False)
|
| 51 |
|
|
|
|
| 52 |
def chat_with_docs(message, history, file_obj):
|
| 53 |
global index, query_engine
|
| 54 |
|
|
|
|
| 55 |
if file_obj is not None:
|
| 56 |
try:
|
| 57 |
pdf_docs = SimpleDirectoryReader(input_files=[file_obj.name]).load_data()
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error processing uploaded PDF: {str(e)}"
|
| 63 |
|
|
|
|
| 64 |
response = query_engine.query(message)
|
| 65 |
return str(response)
|
| 66 |
|
|
|
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
fn=chat_with_docs,
|
| 69 |
title="📚 TechDocs-Knowledge-Engine",
|