Anaghasss commited on
Commit
3e0d532
Β·
verified Β·
1 Parent(s): 3802b5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -45
app.py CHANGED
@@ -2,49 +2,73 @@ import os
2
  import streamlit as st
3
  from git import Repo
4
  import shutil
5
- from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
 
 
 
 
 
 
6
  from llama_index.llms.llama_cpp import LlamaCPP
7
- from llama_index.embeddings import HuggingFaceEmbedding
8
- from llama_index.node_parser import SimpleNodeParser
9
-
10
- st.set_page_config(page_title="πŸ“˜ GitHub Repo Explainer", layout="wide")
11
- st.title("πŸ“˜ GitHub Repository Explainer")
12
-
13
- github_url = st.text_input("Enter GitHub URL:", placeholder="https://github.com/user/repo")
14
-
15
- if st.button("Analyze Repo") and github_url:
16
- try:
17
- # Cleanup old repo
18
- if os.path.exists("repo"):
19
- shutil.rmtree("repo")
20
-
21
- # Clone repo
22
- Repo.clone_from(github_url, "repo")
23
-
24
- # Load model + embeddings
25
- llm = LlamaCPP(
26
- model_path="tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf",
27
- temperature=0.7,
28
- max_new_tokens=512,
29
- context_window=2048,
30
- model_kwargs={"n_gpu_layers": 0},
31
- verbose=True
32
- )
33
-
34
- embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
35
- service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)
36
-
37
- # Load documents and build index
38
- documents = SimpleDirectoryReader("repo").load_data()
39
- parser = SimpleNodeParser()
40
- nodes = parser.get_nodes_from_documents(documents)
41
- index = VectorStoreIndex(nodes, service_context=service_context)
42
-
43
- # Query the index
44
- query_engine = index.as_query_engine()
45
- response = query_engine.query("Explain the purpose, structure, and setup of this repository.")
46
- st.subheader("🧠 Repository Summary")
47
- st.write(str(response))
48
-
49
- except Exception as e:
50
- st.error(f"Error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import streamlit as st
3
  from git import Repo
4
  import shutil
5
+
6
+ from llama_index.core import (
7
+ VectorStoreIndex,
8
+ SimpleDirectoryReader,
9
+ ServiceContext,
10
+ SentenceSplitter,
11
+ )
12
  from llama_index.llms.llama_cpp import LlamaCPP
13
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
14
+
15
+ st.set_page_config(page_title="GitHub Repo Explainer", layout="wide")
16
+ st.title("πŸ“˜ GitHub Repository Explainer (100% Free)")
17
+
18
+ github_url = st.text_input("GitHub URL", placeholder="https://github.com/user/repo")
19
+
20
+ if st.button("Load and Analyze"):
21
+ if github_url:
22
+ try:
23
+ # Clean previous repo if exists
24
+ if os.path.exists("repo"):
25
+ shutil.rmtree("repo")
26
+
27
+ with st.spinner("πŸ“₯ Cloning GitHub repository..."):
28
+ Repo.clone_from(github_url, "repo")
29
+ st.success("βœ… Repo cloned successfully.")
30
+
31
+ with st.spinner("πŸ”§ Loading LLM and embeddings..."):
32
+ llm = LlamaCPP(
33
+ model_path="tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf",
34
+ temperature=0.7,
35
+ max_new_tokens=512,
36
+ context_window=2048,
37
+ generate_kwargs={"top_p": 0.95, "top_k": 50},
38
+ model_kwargs={"n_gpu_layers": 20, "n_batch": 512},
39
+ verbose=True,
40
+ )
41
+
42
+ embed_model = HuggingFaceEmbedding(
43
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
44
+ )
45
+
46
+ service_context = ServiceContext.from_defaults(
47
+ llm=llm,
48
+ embed_model=embed_model,
49
+ node_parser=SentenceSplitter(chunk_size=512, chunk_overlap=50),
50
+ )
51
+
52
+ with st.spinner("πŸ“„ Reading and parsing files..."):
53
+ docs = SimpleDirectoryReader("repo").load_data()
54
+ st.write(f"πŸ“š {len(docs)} documents loaded.")
55
+
56
+ with st.spinner("πŸ” Building index..."):
57
+ index = VectorStoreIndex.from_documents(docs, service_context=service_context)
58
+ query_engine = index.as_query_engine()
59
+
60
+ with st.spinner("🧠 Querying the model..."):
61
+ query = "Explain the purpose, structure, and setup steps of this GitHub repository."
62
+ response = query_engine.query(query)
63
+
64
+ st.subheader("🧾 Repository Summary")
65
+ st.write(str(response))
66
+
67
+ except Exception as e:
68
+ st.error(f"❌ Something went wrong:\n\n{e}")
69
+ else:
70
+ st.warning("⚠️ Please enter a GitHub repo URL.")
71
+
72
+ if st.button("Reset"):
73
+ shutil.rmtree("repo", ignore_errors=True)
74
+ st.experimental_rerun()