ved-app / app.py
PRANTH1304's picture
Update app.py
4616bdb verified
app_code = '''
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM
import chromadb
from sentence_transformers import SentenceTransformer
import torch
import time
st.set_page_config(
page_title="VED โ€” India's AI",
page_icon="๐Ÿ‡ฎ๐Ÿ‡ณ",
layout="centered"
)
st.title("VED ๐Ÿ‡ฎ๐Ÿ‡ณ")
st.caption("India's Own AI โ€” Built by PRANTH1304")
st.divider()
@st.cache_resource
def load_everything():
# Load embedder
embedder = SentenceTransformer("all-MiniLM-L6-v2")
# Load knowledge base
client = chromadb.Client()
collection = client.create_collection("ved_knowledge")
knowledge = [
("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."),
("startup_002", "GST registration is mandatory in India if annual turnover exceeds 20 lakhs. It gives input tax credit which reduces overall tax burden."),
("startup_003", "Y Combinator gives 500000 dollars for 7 percent equity. Apply at ycombinator.com with a working MVP and real users."),
("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."),
("newton_001", "Newton first law states that an object stays at rest or moves at constant velocity unless an external force acts on it."),
("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."),
("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."),
("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)."),
("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."),
("solid_001", "SOLID principles: Single responsibility, Open closed, Liskov substitution, Interface segregation, Dependency inversion. These make code clean and maintainable."),
("gandhi_001", "Mahatma Gandhi led India independence through non-violence. Key movements: Non-Cooperation 1920, Salt March 1930, Quit India 1942."),
("rag_001", "RAG means Retrieval Augmented Generation. Documents stored as embeddings. Relevant chunks retrieved for each query. Model answers using retrieved context."),
("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."),
("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."),
("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."),
]
texts = [item[1] for item in knowledge]
ids = [item[0] for item in knowledge]
embeddings = embedder.encode(texts).tolist()
collection.add(documents=texts, embeddings=embeddings, ids=ids)
# Load model
tokenizer = AutoTokenizer.from_pretrained("ved_mistral")
model = AutoModelForCausalLM.from_pretrained(
"ved_mistral",
torch_dtype=torch.float16,
device_map="auto"
)
return embedder, collection, tokenizer, model
embedder, collection, tokenizer, model = load_everything()
def ask_ved(question):
query_emb = embedder.encode([question]).tolist()
results = collection.query(query_embeddings=query_emb, n_results=1)
context = results["documents"][0][0]
prompt = f"""[INST] You are VED, India AI assistant.
Use ONLY the context below. One complete sentence answer only.
Context: {context}
Question: {question} [/INST]"""
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
input_length = inputs["input_ids"].shape[1]
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=100,
temperature=0.1,
do_sample=False,
repetition_penalty=1.3,
pad_token_id=tokenizer.eos_token_id
)
response = tokenizer.decode(
outputs[0][input_length:],
skip_special_tokens=True
).strip()
if "." in response:
response = response[:response.index(".")+1]
return response.strip()
# Chat interface
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.messages.append({
"role": "assistant",
"content": "Namaste! I am VED โ€” India's AI. Ask me anything about startups, science, history, coding, or India. ๐Ÿ‡ฎ๐Ÿ‡ณ"
})
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
if question := st.chat_input("Ask VED anything..."):
st.session_state.messages.append({"role": "user", "content": question})
with st.chat_message("user"):
st.write(question)
with st.chat_message("assistant"):
with st.spinner("VED is thinking..."):
answer = ask_ved(question)
st.write(answer)
st.session_state.messages.append({"role": "assistant", "content": answer})
st.divider()
col1, col2, col3 = st.columns(3)
col1.metric("Model", "Mistral 7B")
col2.metric("Knowledge", "15 chunks")
col3.metric("Built by", "PRANTH1304")
'''
with open("app.py", "w") as f:
f.write(app_code)
print("app.py created!")
print("Now go to HuggingFace Space: PRANTH1304/ved-app")
print("Replace app.py with this new code")
print("VED will be a full chat interface!")