import gradio as gr import numpy as np import requests import os from sentence_transformers import SentenceTransformer # ============================== # 1. Setup Gemini API # ============================== #client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY")) # ============================== # 2. Load Embedding Model # ============================== embed_model = SentenceTransformer("all-MiniLM-L6-v2") # Load data def load_data(): with open("data.txt", "r") as f: return f.read() # Chunking def chunk_text(text, chunk_size=300): sentences = text.split(". ") chunks = [] current_chunk = "" for sentence in sentences: if len(current_chunk) + len(sentence) < chunk_size: current_chunk += sentence + ". " else: chunks.append(current_chunk.strip()) current_chunk = sentence + ". " if current_chunk: chunks.append(current_chunk.strip()) return chunks # Embed chunks # Convert all documents into vectors def embed_chunks(chunks): return embed_model.encode(chunks) # Retrieve relevant chunks # Retrieve Top-K Chunks (Dynamic chunk) def retrieve(query, chunks, embeddings, top_k=3, threshold=0.3): query_embedding = embed_model.encode([query])[0] scored_chunks = [] # Similarity Function for i, emb in enumerate(embeddings): similarity = np.dot(query_embedding, emb) / ( np.linalg.norm(query_embedding) * np.linalg.norm(emb) ) scored_chunks.append((similarity, chunks[i])) # Sort by similarity scored_chunks.sort(reverse=True) # Filter by threshold filtered = [chunk for score, chunk in scored_chunks if score > threshold] # If nothing passes, fallback to top_k if not filtered: filtered = [chunk for _, chunk in scored_chunks[:top_k]] return filtered[:top_k] # ============================== # Gemini Generation # ============================== #def generate_with_gemini(prompt): # response = client.models.generate_content( # model="gemini-2.0-flash", # ✅ USE THIS (not 1.5) # contents=prompt # ) # return response.text def generate_with_grok(prompt): url = "https://api.x.ai/v1/chat/completions" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {os.environ.get('XAI_API_KEY')}" } payload = { "model": "grok-4-1-fast", "messages": [ {"role": "system", "content": "You are an AI systems expert."}, {"role": "user", "content": prompt} ], "temperature": 0.3 } response = requests.post(url, headers=headers, json=payload) return response.json()["choices"][0]["message"]["content"] # Final RAG pipeline def ask_ai(query): text = load_data() chunks = chunk_text(text) embeddings = embed_chunks(chunks) relevant_chunks = retrieve(query, chunks, embeddings) context = "\n\n".join([f"Context {i+1}: {chunk}" for i, chunk in enumerate(relevant_chunks)]) prompt = f""" You are an AI systems expert. Use ONLY the context below to answer the question. Context: {context} Question: {query} Answer clearly with: 1. Definition 2. How it works 3. Example """ answer = generate_with_grok(prompt) return answer # UI demo = gr.Interface( fn=ask_ai, inputs=gr.Textbox(lines=3, placeholder="Ask anything about AI systems..."), outputs=gr.Textbox(lines=15), title="AIWisdom — Full RAG System", description="Retrieval + LLM reasoning (real AI system)", ) demo.launch()