User → Retrieval → Context → LLM → Final Answer
Browse files
app.py
CHANGED
|
@@ -1,30 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
def load_data():
|
| 11 |
with open("data.txt", "r") as f:
|
| 12 |
return f.read()
|
| 13 |
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def chunk_text(text, chunk_size=200):
|
| 17 |
return [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
|
| 18 |
|
| 19 |
|
| 20 |
-
#
|
| 21 |
def embed_chunks(chunks):
|
| 22 |
-
return
|
| 23 |
|
| 24 |
|
| 25 |
-
#
|
| 26 |
def retrieve(query, chunks, embeddings):
|
| 27 |
-
query_embedding =
|
| 28 |
|
| 29 |
scores = []
|
| 30 |
for i, emb in enumerate(embeddings):
|
|
@@ -37,7 +41,7 @@ def retrieve(query, chunks, embeddings):
|
|
| 37 |
return [chunk for _, chunk in scores[:2]]
|
| 38 |
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def ask_ai(query):
|
| 42 |
text = load_data()
|
| 43 |
chunks = chunk_text(text)
|
|
@@ -45,23 +49,33 @@ def ask_ai(query):
|
|
| 45 |
|
| 46 |
relevant_chunks = retrieve(query, chunks, embeddings)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
response
|
| 53 |
-
response += " ".join(relevant_chunks)
|
| 54 |
|
| 55 |
-
return response
|
| 56 |
|
| 57 |
|
| 58 |
# UI
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=ask_ai,
|
| 61 |
-
inputs=gr.Textbox(lines=3, placeholder="Ask anything..."),
|
| 62 |
outputs=gr.Textbox(lines=15),
|
| 63 |
-
title="AIWisdom —
|
| 64 |
-
description="Step
|
| 65 |
)
|
| 66 |
|
| 67 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from transformers import pipeline
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
# Embedding model
|
| 7 |
+
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 8 |
|
| 9 |
+
# LLM (free, small but works)
|
| 10 |
+
llm = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 11 |
|
| 12 |
+
|
| 13 |
+
# Load data
|
| 14 |
def load_data():
|
| 15 |
with open("data.txt", "r") as f:
|
| 16 |
return f.read()
|
| 17 |
|
| 18 |
|
| 19 |
+
# Chunking
|
| 20 |
def chunk_text(text, chunk_size=200):
|
| 21 |
return [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
|
| 22 |
|
| 23 |
|
| 24 |
+
# Embed chunks
|
| 25 |
def embed_chunks(chunks):
|
| 26 |
+
return embed_model.encode(chunks)
|
| 27 |
|
| 28 |
|
| 29 |
+
# Retrieve relevant chunks
|
| 30 |
def retrieve(query, chunks, embeddings):
|
| 31 |
+
query_embedding = embed_model.encode([query])[0]
|
| 32 |
|
| 33 |
scores = []
|
| 34 |
for i, emb in enumerate(embeddings):
|
|
|
|
| 41 |
return [chunk for _, chunk in scores[:2]]
|
| 42 |
|
| 43 |
|
| 44 |
+
# Final RAG pipeline
|
| 45 |
def ask_ai(query):
|
| 46 |
text = load_data()
|
| 47 |
chunks = chunk_text(text)
|
|
|
|
| 49 |
|
| 50 |
relevant_chunks = retrieve(query, chunks, embeddings)
|
| 51 |
|
| 52 |
+
context = "\n".join(relevant_chunks)
|
| 53 |
+
|
| 54 |
+
prompt = f"""
|
| 55 |
+
You are an AI systems expert.
|
| 56 |
+
|
| 57 |
+
Use the context below to answer the question.
|
| 58 |
+
|
| 59 |
+
Context:
|
| 60 |
+
{context}
|
| 61 |
+
|
| 62 |
+
Question: {query}
|
| 63 |
+
|
| 64 |
+
Answer clearly:
|
| 65 |
+
"""
|
| 66 |
|
| 67 |
+
response = llm(prompt, max_new_tokens=200)
|
|
|
|
| 68 |
|
| 69 |
+
return response[0]["generated_text"]
|
| 70 |
|
| 71 |
|
| 72 |
# UI
|
| 73 |
demo = gr.Interface(
|
| 74 |
fn=ask_ai,
|
| 75 |
+
inputs=gr.Textbox(lines=3, placeholder="Ask anything about AI systems..."),
|
| 76 |
outputs=gr.Textbox(lines=15),
|
| 77 |
+
title="AIWisdom — Full RAG System",
|
| 78 |
+
description="Step 4: Retrieval + LLM reasoning (real AI system)",
|
| 79 |
)
|
| 80 |
|
| 81 |
demo.launch()
|