006aman commited on
Commit
bcbd83d
·
1 Parent(s): eeb09e1

Fix build errors and include document corpus

Browse files
.gitignore CHANGED
@@ -1,5 +1,6 @@
1
  # Data and databases
2
- data/
 
3
  .chroma/
4
  *.db
5
  *.sqlite3
 
1
  # Data and databases
2
+ # data/ # ALLOW data/ to be uploaded for HF Spaces
3
+ data/chroma_db/
4
  .chroma/
5
  *.db
6
  *.sqlite3
app.py CHANGED
@@ -1,30 +1,74 @@
1
  import os
2
  import sys
 
3
 
4
  # Add src to the Python path
 
 
5
  sys.path.insert(0, "src")
6
 
7
- from iks_rag.ui.gradio_app import create_interface
8
- from iks_rag.rag_system import create_rag_system
9
-
10
  print("🚀 Initializing IKS RAG System on HuggingFace Spaces...")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Make sure Gemini key is loaded from space secrets
13
  api_key = os.environ.get("GOOGLE_API_KEY", "")
14
  if not api_key:
15
  print("⚠️ WARNING: GOOGLE_API_KEY not found in environment variables!")
 
16
 
17
  # Create RAG system using our default config
18
- # We use the default.yaml which is already configured for Gemini
19
- rag_system = create_rag_system("configs/rag/default.yaml")
 
 
 
20
 
21
- print("📚 Loading documents (from local ChromaDB)...")
22
- rag_system.load_documents()
 
 
 
 
23
 
24
- stats = rag_system.get_stats()
25
- print(f"✅ Loaded {stats['documents_loaded']} document chunks")
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # Launch the Gradio Interface
28
- print("🌐 Launching Gradio...")
29
- demo = create_interface(rag_system)
30
- demo.launch()
 
 
 
 
 
 
 
1
  import os
2
  import sys
3
+ from pathlib import Path
4
 
5
  # Add src to the Python path
6
+ # HF Spaces has the repo content in the root.
7
+ # Our package is in src/iks_rag
8
  sys.path.insert(0, "src")
9
 
 
 
 
10
  print("🚀 Initializing IKS RAG System on HuggingFace Spaces...")
11
 
12
+ # Check environment
13
+ print(f"Python Version: {sys.version}")
14
+ print(f"CWD: {os.getcwd()}")
15
+
16
+ # Import after path is set
17
+ try:
18
+ from iks_rag.ui.gradio_app import create_interface
19
+ from iks_rag.rag_system import create_rag_system
20
+ print("✅ Successfully imported iks_rag modules")
21
+ except ImportError as e:
22
+ print(f"❌ ERROR: Failed to import iks_rag modules: {e}")
23
+ print(f"Python path: {sys.path}")
24
+ # Create a simple fallback UI if imports fail to show the error on the page
25
+ import gradio as gr
26
+ demo = gr.Interface(fn=lambda x: f"Import Error: {e}", inputs="text", outputs="text")
27
+ demo.launch()
28
+ sys.exit(1)
29
+
30
  # Make sure Gemini key is loaded from space secrets
31
  api_key = os.environ.get("GOOGLE_API_KEY", "")
32
  if not api_key:
33
  print("⚠️ WARNING: GOOGLE_API_KEY not found in environment variables!")
34
+ print("Please set GOOGLE_API_KEY in the 'Settings > Secrets' section of your Space.")
35
 
36
  # Create RAG system using our default config
37
+ config_path = "configs/rag/default.yaml"
38
+ if not os.path.exists(config_path):
39
+ print(f"❌ ERROR: Config file not found at {config_path}")
40
+ else:
41
+ print(f"✅ Found config at {config_path}")
42
 
43
+ try:
44
+ rag_system = create_rag_system(config_path)
45
+ print("✅ RAG system initialized")
46
+ except Exception as e:
47
+ print(f"❌ ERROR initializing RAG system: {e}")
48
+ rag_system = None
49
 
50
+ # Check data and load
51
+ if rag_system:
52
+ docs_path = "data/documents"
53
+ if not os.path.exists(docs_path) or not os.listdir(docs_path):
54
+ print(f"⚠️ WARNING: No documents found in {docs_path}")
55
+ else:
56
+ print(f"📚 Loading documents from {docs_path}...")
57
+
58
+ try:
59
+ rag_system.load_documents()
60
+ stats = rag_system.get_stats()
61
+ print(f"✅ Loaded {stats['documents_loaded']} document chunks into ChromaDB")
62
+ except Exception as e:
63
+ print(f"❌ ERROR loading documents: {str(e)}")
64
 
65
  # Launch the Gradio Interface
66
+ print("🌐 Launching Gradio Interface...")
67
+ if rag_system:
68
+ demo = create_interface(rag_system)
69
+ else:
70
+ import gradio as gr
71
+ demo = gr.Interface(fn=lambda x: "RAG System failed to initialize", inputs="text", outputs="text")
72
+
73
+ if __name__ == "__main__":
74
+ demo.launch()
requirements.txt CHANGED
@@ -4,10 +4,10 @@ llama-index-embeddings-huggingface>=0.5.0
4
  llama-index-vector-stores-chroma>=0.4.0
5
  llama-index-readers-file>=0.1.0
6
  chromadb>=0.6.0
7
- sentence-transformers>=3.0.0,<4.0.0
8
- transformers<4.42.0
9
- numpy>=1.26.0,<2.0.0
10
- gradio>=5.0.0
11
  google-generativeai>=0.8.0
12
  pydantic-settings>=2.6.0
13
  pydantic>=2.9.0
 
4
  llama-index-vector-stores-chroma>=0.4.0
5
  llama-index-readers-file>=0.1.0
6
  chromadb>=0.6.0
7
+ sentence-transformers>=3.0.0
8
+ transformers>=4.42.0
9
+ numpy>=1.26.0
10
+ gradio>=6.13.0
11
  google-generativeai>=0.8.0
12
  pydantic-settings>=2.6.0
13
  pydantic>=2.9.0
src/iks_rag/ui/__pycache__/gradio_app.cpython-312.pyc CHANGED
Binary files a/src/iks_rag/ui/__pycache__/gradio_app.cpython-312.pyc and b/src/iks_rag/ui/__pycache__/gradio_app.cpython-312.pyc differ
 
src/iks_rag/ui/gradio_app.py CHANGED
@@ -4,6 +4,7 @@ Provides a chat interface for students to ask IKS questions.
4
  """
5
 
6
  from pathlib import Path
 
7
 
8
  # Load .env FIRST — must happen before iks_rag imports so API keys are available
9
  try:
@@ -14,10 +15,65 @@ except ImportError:
14
 
15
  import gradio as gr
16
 
17
- from iks_rag.config import load_config
18
  from iks_rag.rag_system import RAGSystem
19
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def create_interface(rag_system: RAGSystem) -> gr.Blocks:
22
  """Create Gradio interface.
23
 
@@ -83,50 +139,16 @@ def create_interface(rag_system: RAGSystem) -> gr.Blocks:
83
  # Examples section (hidden by default)
84
  examples_md = gr.Markdown(visible=False)
85
 
86
- def format_examples():
87
- """Format example questions."""
88
- examples = config.ui.examples
89
- md = "### Example Questions\n\n"
90
- for i, ex in enumerate(examples, 1):
91
- md += f"{i}. {ex}\n\n"
92
- return md
93
-
94
  def respond(message: str, chat_history: list):
95
- """Process user message and generate response."""
96
- if not message.strip():
97
- return "", chat_history
98
-
99
- # Add user message (Gradio 6.0 message format)
100
- chat_history.append({"role": "user", "content": message})
101
-
102
- try:
103
- # Query RAG system
104
- result = rag_system.query(message)
105
- answer = result["answer"]
106
-
107
- # Format sources
108
- sources_text = "\n\n**Sources:**\n"
109
- for i, source in enumerate(result["sources"][:3], 1):
110
- file_name = source["metadata"].get("file_name", "Unknown")
111
- sources_text += f"\n{i}. {file_name}"
112
-
113
- full_response = f"{answer}{sources_text}"
114
-
115
- chat_history.append({"role": "assistant", "content": full_response})
116
- return "", chat_history
117
-
118
- except Exception as e:
119
- error_msg = f"❌ Error: {str(e)}\n\nPlease ensure:\n- Ollama is running (ollama serve)\n- Documents are loaded"
120
- chat_history.append({"role": "assistant", "content": error_msg})
121
- return "", chat_history
122
 
123
  def clear_chat():
124
  """Clear chat history."""
125
- return None
126
 
127
  def toggle_examples():
128
  """Toggle examples visibility."""
129
- return gr.update(visible=True, value=format_examples())
130
 
131
  # Event handlers
132
  submit.click(respond, [msg, chatbot], [msg, chatbot])
 
4
  """
5
 
6
  from pathlib import Path
7
+ from typing import Any
8
 
9
  # Load .env FIRST — must happen before iks_rag imports so API keys are available
10
  try:
 
15
 
16
  import gradio as gr
17
 
18
+ from iks_rag.config import load_config, RAGConfig
19
  from iks_rag.rag_system import RAGSystem
20
 
21
 
22
+ def format_examples(config: RAGConfig) -> str:
23
+ """Format example questions.
24
+
25
+ Args:
26
+ config: RAG configuration
27
+
28
+ Returns:
29
+ Formatted markdown string
30
+ """
31
+ examples = config.ui.examples
32
+ md = "### Example Questions\n\n"
33
+ for i, ex in enumerate(examples, 1):
34
+ md += f"{i}. {ex}\n\n"
35
+ return md
36
+
37
+
38
+ def handle_response(message: str, chat_history: list, rag_system: RAGSystem) -> tuple[str, list]:
39
+ """Process user message and generate response.
40
+
41
+ Args:
42
+ message: User input message
43
+ chat_history: Current chat history
44
+ rag_system: RAG system instance
45
+
46
+ Returns:
47
+ Tuple of (empty string, updated chat history)
48
+ """
49
+ if not message.strip():
50
+ return "", chat_history
51
+
52
+ # Add user message (Gradio 6.0 message format)
53
+ chat_history.append({"role": "user", "content": message})
54
+
55
+ try:
56
+ # Query RAG system
57
+ result = rag_system.query(message)
58
+ answer = result["answer"]
59
+
60
+ # Format sources
61
+ sources_text = "\n\n**Sources:**\n"
62
+ for i, source in enumerate(result["sources"][:3], 1):
63
+ file_name = source["metadata"].get("file_name", "Unknown")
64
+ sources_text += f"\n{i}. {file_name}"
65
+
66
+ full_response = f"{answer}{sources_text}"
67
+
68
+ chat_history.append({"role": "assistant", "content": full_response})
69
+ return "", chat_history
70
+
71
+ except Exception as e:
72
+ error_msg = f"❌ Error: {str(e)}\n\nPlease ensure:\n- API keys are set\n- Documents are loaded"
73
+ chat_history.append({"role": "assistant", "content": error_msg})
74
+ return "", chat_history
75
+
76
+
77
  def create_interface(rag_system: RAGSystem) -> gr.Blocks:
78
  """Create Gradio interface.
79
 
 
139
  # Examples section (hidden by default)
140
  examples_md = gr.Markdown(visible=False)
141
 
 
 
 
 
 
 
 
 
142
  def respond(message: str, chat_history: list):
143
+ return handle_response(message, chat_history, rag_system)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  def clear_chat():
146
  """Clear chat history."""
147
+ return []
148
 
149
  def toggle_examples():
150
  """Toggle examples visibility."""
151
+ return gr.update(visible=True, value=format_examples(config))
152
 
153
  # Event handlers
154
  submit.click(respond, [msg, chatbot], [msg, chatbot])