lateesha-bhatia commited on
Commit
9b3f136
·
verified ·
1 Parent(s): 66ec742

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import gradio as gr
4
+ from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
5
+ from llama_index.llms.groq import Groq
6
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
7
+ from llama_index.vector_stores.qdrant import QdrantVectorStore
8
+ from qdrant_client import QdrantClient
9
+ from qdrant_client.models import Distance, VectorParams
10
+
11
+ Settings.llm = Groq(model="llama3-8b-8192", api_key=os.environ.get("GROQ_API_KEY"))
12
+ Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-large-en-v1.5")
13
+ Settings.chunk_size = 512
14
+ Settings.chunk_overlap = 50
15
+
16
+ client = QdrantClient(location=":memory:")
17
+ client.create_collection(
18
+ collection_name="tech_docs",
19
+ vectors_config=VectorParams(size=1024, distance=Distance.COSINE),
20
+ )
21
+ vector_store = QdrantVectorStore(client=client, collection_name="tech_docs")
22
+ index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
23
+
24
+ TARGET_REPO = "https://github.com/tiangolo/fastapi.git"
25
+ clone_path = "/tmp/fastapi_docs"
26
+
27
+ if not os.path.exists(clone_path):
28
+ print(f"Cloning {TARGET_REPO}...")
29
+ subprocess.run(["git", "clone", "--depth", "1", TARGET_REPO, clone_path])
30
+
31
+ print("Indexing repository documentation...")
32
+ try:
33
+ documents = SimpleDirectoryReader(
34
+ input_dir=clone_path,
35
+ required_exts=[".md", ".mdx"],
36
+ recursive=True
37
+ ).load_data()
38
+ # Ingest a safe subset to fit comfortably within free RAM constraints
39
+ for doc in documents[:200]:
40
+ index.insert(doc)
41
+ print("GitHub repository indexed successfully!")
42
+ except Exception as e:
43
+ print(f"Error indexing GitHub repo: {e}")
44
+
45
+ query_engine = index.as_query_engine(streaming=False)
46
+
47
+ # 4. Define Response Function for Gradio Chat Interface
48
+ def chat_with_docs(message, history, file_obj):
49
+ global index, query_engine
50
+
51
+ # If user uploaded a new PDF, ingest it dynamically into the active session
52
+ if file_obj is not None:
53
+ try:
54
+ pdf_docs = SimpleDirectoryReader(input_files=[file_obj.name]).load_data()
55
+ for doc in pdf_docs:
56
+ index.insert(doc)
57
+ query_engine = index.as_query_engine(streaming=False)
58
+ except Exception as e:
59
+ return f"Error processing uploaded PDF: {str(e)}"
60
+
61
+ # Query the RAG engine using Groq acceleration
62
+ response = query_engine.query(message)
63
+ return str(response)
64
+
65
+ # 5. Build Gradio Chat UI Layout
66
+ demo = gr.ChatInterface(
67
+ fn=chat_with_docs,
68
+ title="📚 TechDocs-Knowledge-Engine",
69
+ description="Ask technical questions about the indexed codebase or upload your own technical PDF using the file attachment icon below.",
70
+ additional_inputs=[
71
+ gr.File(label="Upload Technical PDF (Optional)", file_types=[".pdf"])
72
+ ],
73
+ textbox=gr.Textbox(placeholder="Ask a technical question about code or architecture...", container=False, scale=7),
74
+ theme="soft"
75
+ )
76
+
77
+ if __name__ == "__main__":
78
+ demo.launch(server_name="0.0.0.0", server_port=7860)