Uploading project 2 to Hugging Face Hub
Browse files- README.md +58 -8
- app.py +62 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -1,12 +1,62 @@
|
|
| 1 |
-
|
| 2 |
-
title: Domain Specific
|
| 3 |
-
emoji:
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.5.1
|
| 8 |
app_file: app.py
|
| 9 |
-
pinned:
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
| 1 |
+
|
| 2 |
+
title: Domain Specific RAG With a Small Language Model
|
| 3 |
+
emoji: π
|
| 4 |
+
description: Search for Python tasks to retrieve verified solutions and test cases.
|
| 5 |
+
colorFrom: blue
|
| 6 |
+
colorTo: green
|
| 7 |
sdk: gradio
|
| 8 |
sdk_version: 6.5.1
|
| 9 |
app_file: app.py
|
| 10 |
+
pinned: true
|
| 11 |
+
|
| 12 |
+
π Technical Code Assistant: RAG with MBPP & Phi-3
|
| 13 |
+
|
| 14 |
+
π Overview
|
| 15 |
+
|
| 16 |
+
This project is a domain-specific Retrieval-Augmented Generation (RAG) system designed to act as a technical coding assistant. Unlike generic LLMs that may generate syntax-error-prone code, this system retrieves hand-verified solutions from Google's MBPP (Mostly Basic Python Problems) dataset and uses Microsoft Phi-3 to provide grounded, explained answers.
|
| 17 |
+
|
| 18 |
+
π οΈ Technical Stack
|
| 19 |
+
|
| 20 |
+
Model: Phi-3-mini-4k-instruct (3.8B parameters) β chosen for its high efficiency and reasoning capabilities in a small footprint.
|
| 21 |
+
|
| 22 |
+
Vector Store: FAISS (Facebook AI Similarity Search) for high-speed local vector indexing.
|
| 23 |
+
|
| 24 |
+
Embeddings: sentence-transformers/all-MiniLM-L6-v2 β optimized for asymmetric semantic search (matching natural language queries to code tasks).
|
| 25 |
+
|
| 26 |
+
Data Source: google-research-datasets/mbpp (Sanitized version) β a gold-standard benchmark for Python coding tasks.
|
| 27 |
+
|
| 28 |
+
π§ The Pipeline: How it Works
|
| 29 |
+
|
| 30 |
+
* The application follows a rigorous retrieval-first architecture to ensure code reliability:
|
| 31 |
+
|
| 32 |
+
* Semantic Retrieval: The user enters a coding task (e.g., "Find the area of a circle"). The system encodes this query into a vector and searches the FAISS index for the most semantically similar verified task in the MBPP dataset.
|
| 33 |
+
|
| 34 |
+
* Context Augmentation: The system retrieves the original human-written Python function and the associated unit tests for that task.
|
| 35 |
+
|
| 36 |
+
* Grounded Generation: Phi-3 receives the retrieved code as "ground truth" and generates an explanation or adaptation based only on that verified logic.
|
| 37 |
+
|
| 38 |
+
* Verification UI: The interface displays the retrieved code in a dedicated gr.Code block and lists the test cases to prove the solution is functional.
|
| 39 |
+
|
| 40 |
+
π― Key Features for Portfolio
|
| 41 |
+
|
| 42 |
+
* Small Language Model (SLM) Optimization: Demonstrates an ability to deploy high-performing systems on limited hardware (Free Tier CPU) by selecting the right model-to-task ratio.
|
| 43 |
+
|
| 44 |
+
* Benchmark Grounding: Uses the MBPP dataset to eliminate hallucinations common in open-domain LLMs.
|
| 45 |
+
|
| 46 |
+
* Technical UI Design: Implemented syntax highlighting and structured JSON outputs for a developer-centric user experience.
|
| 47 |
+
|
| 48 |
+
π¨βπ» How to Test
|
| 49 |
+
|
| 50 |
+
Enter a Python task in the text box (e.g., "Write a function to check if a number is prime").
|
| 51 |
+
|
| 52 |
+
Click "Find Solution".
|
| 53 |
+
|
| 54 |
+
Review the Verified Python Code and run the provided Test Cases locally to verify the output!
|
| 55 |
+
|
| 56 |
+
π Future Enhancements
|
| 57 |
+
|
| 58 |
+
[ ] Hybrid Search: Combine FAISS semantic search with BM25 keyword matching to better handle specific library names.
|
| 59 |
+
|
| 60 |
+
[ ] Multi-Dataset Support: Expand the knowledge base to include LeetCode and HumanEval datasets.
|
| 61 |
|
| 62 |
+
[ ] Interactive Execution: Integrate a secure sandboxed Python interpreter to run the retrieved test cases directly in the browser.
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
+
import torch
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
|
| 9 |
+
# setup model
|
| 10 |
+
model_id = "microsoft/Phi-3-mini-4k-instruct"
|
| 11 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 12 |
+
|
| 13 |
+
# load and index MBPP dataset
|
| 14 |
+
print(f"[INFO] Loading MBPP Dataset...")
|
| 15 |
+
dataset = load_dataset("google-research-datasets/mbpp",
|
| 16 |
+
"sanitized",
|
| 17 |
+
split="test")
|
| 18 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 19 |
+
|
| 20 |
+
# index the task text but store the whole dictionary as meta data
|
| 21 |
+
vector_db = FAISS.from_texts(texts=[item['prompt'] for item in dataset],
|
| 22 |
+
embedding=embeddings,
|
| 23 |
+
metadatas=[{"code": item['code'],
|
| 24 |
+
"tests": item['test_list']} for item in dataset])
|
| 25 |
+
|
| 26 |
+
def lookup_code(query):
|
| 27 |
+
# find the top match
|
| 28 |
+
docs = vector_db.similarity_search(query, k=1)
|
| 29 |
+
if not docs:
|
| 30 |
+
return "No match found.", "N/A", []
|
| 31 |
+
|
| 32 |
+
best_match = docs[0]
|
| 33 |
+
task_description = best_match.page_content
|
| 34 |
+
retrieved_code = best_match.metadata['code']
|
| 35 |
+
test_cases = "\n".join(best_match.metadata['tests'])
|
| 36 |
+
|
| 37 |
+
return task_description, retrieved_code, test_cases
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 40 |
+
gr.Markdown("# 🐍 Technical Code RAG (MBPP)")
|
| 41 |
+
gr.Markdown("Search for Python tasks to retrieve verified solutions and test cases.")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
query_input = gr.Textbox(label="Enter Task (e.g., 'find the area of circle')",
|
| 45 |
+
placeholder="Search...")
|
| 46 |
+
search_btn = gr.Button("Find Solution",
|
| 47 |
+
variant="primary")
|
| 48 |
+
|
| 49 |
+
with gr.Column():
|
| 50 |
+
out_desc = gr.Markdown(label="Task Description")
|
| 51 |
+
|
| 52 |
+
out_code = gr.Code(label="Verified Python Code",
|
| 53 |
+
language="python")
|
| 54 |
+
|
| 55 |
+
out_tests = gr.Textbox(label="Validation Test Cases",
|
| 56 |
+
lines=3)
|
| 57 |
+
|
| 58 |
+
search_btn.click(fn=lookup_code,
|
| 59 |
+
inputs=query_input,
|
| 60 |
+
outputs=[out_desc, out_code, out_tests])
|
| 61 |
+
|
| 62 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
sentence-transformers
|
| 4 |
+
faiss.cpu
|
| 5 |
+
langchain
|
| 6 |
+
langchain_community
|
| 7 |
+
gradio
|