File size: 1,280 Bytes
6d24925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import gradio as gr
from components.indexers.news_indexer import (
    build_news_index,
    load_news_index
)
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.core.settings import Settings

# 💥 Block OpenAI LLM usage
Settings.llm = None

DATA_DIR = "data/raw"
INDEX_DIR = "storage/index"

# Create dummy file if needed
if not os.path.exists(DATA_DIR):
    os.makedirs(DATA_DIR, exist_ok=True)
    with open(os.path.join(DATA_DIR, "sample.txt"), "w") as f:
        f.write("Sample news: India’s Prime Minister spoke today about the tech economy boom.")

# Build index if missing
if not os.path.exists(os.path.join(INDEX_DIR, "docstore.json")):
    print("📦 Index not found — building it now...")
    build_news_index(data_dir=DATA_DIR, index_dir=INDEX_DIR)

# Load index safely
print("📥 Loading index...")
index = load_news_index(INDEX_DIR)
query_engine = index.as_query_engine()

def query_news(question):
    response = query_engine.query(question)
    return str(response)

iface = gr.Interface(
    fn=query_news,
    inputs=gr.Textbox(label="Ask about the news"),
    outputs=gr.Textbox(label="Answer"),
    title="LucidFeed: News Assistant",
    description="Ask questions about the latest headlines."
)

iface.launch()