Update VED-app.py
Browse files- VED-app.py +133 -0
VED-app.py
CHANGED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
app_code = '''
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import chromadb
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
import torch
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="VED — India's AI",
|
| 11 |
+
page_icon="🇮🇳",
|
| 12 |
+
layout="centered"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
st.title("VED 🇮🇳")
|
| 16 |
+
st.caption("India's Own AI — Built by PRANTH1304")
|
| 17 |
+
st.divider()
|
| 18 |
+
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_everything():
|
| 21 |
+
# Load embedder
|
| 22 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 23 |
+
|
| 24 |
+
# Load knowledge base
|
| 25 |
+
client = chromadb.Client()
|
| 26 |
+
collection = client.create_collection("ved_knowledge")
|
| 27 |
+
|
| 28 |
+
knowledge = [
|
| 29 |
+
("startup_001", "To register a startup in India, visit startupindia.gov.in, get DPIIT recognition, and enjoy 3 years tax exemption under Section 80-IAC."),
|
| 30 |
+
("startup_002", "GST registration is mandatory in India if annual turnover exceeds 20 lakhs. It gives input tax credit which reduces overall tax burden."),
|
| 31 |
+
("startup_003", "Y Combinator gives 500000 dollars for 7 percent equity. Apply at ycombinator.com with a working MVP and real users."),
|
| 32 |
+
("startup_004", "Startup India Seed Fund gives up to 20 lakhs free money to early stage Indian startups. No equity taken. Apply at startupindia.gov.in."),
|
| 33 |
+
("newton_001", "Newton first law states that an object stays at rest or moves at constant velocity unless an external force acts on it."),
|
| 34 |
+
("tajmahal_001", "The Taj Mahal was built by Mughal Emperor Shah Jahan between 1632 and 1653 in memory of his wife Mumtaz Mahal in Agra."),
|
| 35 |
+
("bigo_001", "Big O notation measures algorithm efficiency. O(1) is constant time. O(n) is linear. O(log n) is logarithmic. O(n squared) is quadratic worst case."),
|
| 36 |
+
("binary_001", "Binary search works by comparing the middle element of a sorted array with the target. Search left half if smaller, right half if larger. Time complexity O(log n)."),
|
| 37 |
+
("photo_001", "Photosynthesis is the process where plants use sunlight, water, and carbon dioxide to produce food. Chlorophyll absorbs sunlight. Oxygen is released as byproduct."),
|
| 38 |
+
("solid_001", "SOLID principles: Single responsibility, Open closed, Liskov substitution, Interface segregation, Dependency inversion. These make code clean and maintainable."),
|
| 39 |
+
("gandhi_001", "Mahatma Gandhi led India independence through non-violence. Key movements: Non-Cooperation 1920, Salt March 1930, Quit India 1942."),
|
| 40 |
+
("rag_001", "RAG means Retrieval Augmented Generation. Documents stored as embeddings. Relevant chunks retrieved for each query. Model answers using retrieved context."),
|
| 41 |
+
("recursion_001", "Recursion is when a function calls itself. Every recursive function needs a base case to stop. Example: factorial of n equals n times factorial of n minus 1."),
|
| 42 |
+
("india_001", "India is the world largest democracy with 1.4 billion people. Parliamentary system with Lok Sabha and Rajya Sabha. Prime Minister is head of government."),
|
| 43 |
+
("python_001", "Python list comprehension: [x for x in range(10) if x percent 2 == 0] gives even numbers. Faster and cleaner than for loops."),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
texts = [item[1] for item in knowledge]
|
| 47 |
+
ids = [item[0] for item in knowledge]
|
| 48 |
+
embeddings = embedder.encode(texts).tolist()
|
| 49 |
+
collection.add(documents=texts, embeddings=embeddings, ids=ids)
|
| 50 |
+
|
| 51 |
+
# Load model
|
| 52 |
+
tokenizer = AutoTokenizer.from_pretrained("ved_mistral")
|
| 53 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
+
"ved_mistral",
|
| 55 |
+
torch_dtype=torch.float16,
|
| 56 |
+
device_map="auto"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
return embedder, collection, tokenizer, model
|
| 60 |
+
|
| 61 |
+
embedder, collection, tokenizer, model = load_everything()
|
| 62 |
+
|
| 63 |
+
def ask_ved(question):
|
| 64 |
+
query_emb = embedder.encode([question]).tolist()
|
| 65 |
+
results = collection.query(query_embeddings=query_emb, n_results=1)
|
| 66 |
+
context = results["documents"][0][0]
|
| 67 |
+
|
| 68 |
+
prompt = f"""[INST] You are VED, India AI assistant.
|
| 69 |
+
Use ONLY the context below. One complete sentence answer only.
|
| 70 |
+
|
| 71 |
+
Context: {context}
|
| 72 |
+
Question: {question} [/INST]"""
|
| 73 |
+
|
| 74 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 75 |
+
input_length = inputs["input_ids"].shape[1]
|
| 76 |
+
|
| 77 |
+
with torch.no_grad():
|
| 78 |
+
outputs = model.generate(
|
| 79 |
+
**inputs,
|
| 80 |
+
max_new_tokens=100,
|
| 81 |
+
temperature=0.1,
|
| 82 |
+
do_sample=False,
|
| 83 |
+
repetition_penalty=1.3,
|
| 84 |
+
pad_token_id=tokenizer.eos_token_id
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
response = tokenizer.decode(
|
| 88 |
+
outputs[0][input_length:],
|
| 89 |
+
skip_special_tokens=True
|
| 90 |
+
).strip()
|
| 91 |
+
|
| 92 |
+
if "." in response:
|
| 93 |
+
response = response[:response.index(".")+1]
|
| 94 |
+
|
| 95 |
+
return response.strip()
|
| 96 |
+
|
| 97 |
+
# Chat interface
|
| 98 |
+
if "messages" not in st.session_state:
|
| 99 |
+
st.session_state.messages = []
|
| 100 |
+
st.session_state.messages.append({
|
| 101 |
+
"role": "assistant",
|
| 102 |
+
"content": "Namaste! I am VED — India's AI. Ask me anything about startups, science, history, coding, or India. 🇮🇳"
|
| 103 |
+
})
|
| 104 |
+
|
| 105 |
+
for message in st.session_state.messages:
|
| 106 |
+
with st.chat_message(message["role"]):
|
| 107 |
+
st.write(message["content"])
|
| 108 |
+
|
| 109 |
+
if question := st.chat_input("Ask VED anything..."):
|
| 110 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
| 111 |
+
with st.chat_message("user"):
|
| 112 |
+
st.write(question)
|
| 113 |
+
|
| 114 |
+
with st.chat_message("assistant"):
|
| 115 |
+
with st.spinner("VED is thinking..."):
|
| 116 |
+
answer = ask_ved(question)
|
| 117 |
+
st.write(answer)
|
| 118 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 119 |
+
|
| 120 |
+
st.divider()
|
| 121 |
+
col1, col2, col3 = st.columns(3)
|
| 122 |
+
col1.metric("Model", "Mistral 7B")
|
| 123 |
+
col2.metric("Knowledge", "15 chunks")
|
| 124 |
+
col3.metric("Built by", "PRANTH1304")
|
| 125 |
+
'''
|
| 126 |
+
|
| 127 |
+
with open("app.py", "w") as f:
|
| 128 |
+
f.write(app_code)
|
| 129 |
+
|
| 130 |
+
print("app.py created!")
|
| 131 |
+
print("Now go to HuggingFace Space: PRANTH1304/ved-app")
|
| 132 |
+
print("Replace app.py with this new code")
|
| 133 |
+
print("VED will be a full chat interface!")
|