Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,117 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# PromptPilot: Multi-Model
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
-
from transformers import (
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
import torch
|
| 7 |
|
| 8 |
-
st.set_page_config(page_title="PromptPilot
|
| 9 |
-
st.title("π€ PromptPilot
|
| 10 |
|
| 11 |
-
st.markdown(""
|
| 12 |
-
**MVP Chatbot Demo for AML-3304**
|
| 13 |
-
Supports:
|
| 14 |
-
- Basic Code Generation
|
| 15 |
-
- General Q&A
|
| 16 |
-
- Bayesian-style Q&A
|
| 17 |
-
- Creative Writing
|
| 18 |
-
- DeepSeekβR1 RAG (Retrieval-Augmented Q&A)
|
| 19 |
-
""")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
|
|
|
| 24 |
cols = st.columns(5)
|
| 25 |
-
|
| 26 |
-
("
|
| 27 |
-
("π General Q&A",
|
| 28 |
-
("π Bayesian Q&A",
|
| 29 |
-
("
|
| 30 |
-
("π
|
| 31 |
]
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
st.markdown(f"### π Current Mode: **{st.session_state.mode}**")
|
| 37 |
-
user_input = st.text_input("Enter your prompt/question:")
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
@st.cache_resource
|
| 40 |
def load_codegen():
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
@st.cache_resource
|
| 45 |
def load_flan():
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
@st.cache_resource
|
| 50 |
-
def load_falcon():
|
| 51 |
-
return (AutoTokenizer.from_pretrained("tiiuae/falcon-rw-1b"),
|
| 52 |
-
AutoModelForCausalLM.from_pretrained("tiiuae/falcon-rw-1b"))
|
| 53 |
|
| 54 |
@st.cache_resource
|
| 55 |
-
def
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return tokenizer, model
|
| 60 |
|
|
|
|
| 61 |
if user_input:
|
| 62 |
with st.spinner("Generating response..."):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if mode == 'Code Generator':
|
| 66 |
tok, mod = load_codegen()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
elif mode
|
| 72 |
tok, mod = load_flan()
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# PromptPilot: Unified Multi-Model Chatbot (AML-3304 Final Submission)
|
| 3 |
+
|
| 4 |
import streamlit as st
|
| 5 |
+
from transformers import (
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
AutoModelForCausalLM,
|
| 8 |
+
AutoModelForSeq2SeqLM
|
| 9 |
+
)
|
| 10 |
import torch
|
| 11 |
|
| 12 |
+
st.set_page_config(page_title="PromptPilot", layout="centered")
|
| 13 |
+
st.title("π€ PromptPilot - Unified AI Chatbot")
|
| 14 |
|
| 15 |
+
st.markdown("AML-3304 Assignment: From Tokens to Transformers")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Track current mode
|
| 18 |
+
if "mode" not in st.session_state:
|
| 19 |
+
st.session_state.mode = "Code Generator"
|
| 20 |
|
| 21 |
+
# Mode selection buttons
|
| 22 |
cols = st.columns(5)
|
| 23 |
+
mode_labels = [
|
| 24 |
+
("π¨βπ» Code Generator", "Code Generator"),
|
| 25 |
+
("π General Q&A", "General Q&A"),
|
| 26 |
+
("π Bayesian Q&A", "Bayesian Q&A"),
|
| 27 |
+
("π DeepSeek Coder", "DeepSeek"),
|
| 28 |
+
("π Tokenizer Introspector", "Tokenizer")
|
| 29 |
]
|
| 30 |
+
|
| 31 |
+
for i, (label, mode) in enumerate(mode_labels):
|
| 32 |
+
if cols[i].button(label):
|
| 33 |
+
st.session_state.mode = mode
|
| 34 |
|
| 35 |
st.markdown(f"### π Current Mode: **{st.session_state.mode}**")
|
|
|
|
| 36 |
|
| 37 |
+
# Input field
|
| 38 |
+
user_input = st.text_area("Enter your prompt/question:", height=100)
|
| 39 |
+
|
| 40 |
+
# Cached model loaders
|
| 41 |
@st.cache_resource
|
| 42 |
def load_codegen():
|
| 43 |
+
tok = AutoTokenizer.from_pretrained("Salesforce/codegen-350M-mono")
|
| 44 |
+
mod = AutoModelForCausalLM.from_pretrained("Salesforce/codegen-350M-mono")
|
| 45 |
+
return tok, mod
|
| 46 |
|
| 47 |
@st.cache_resource
|
| 48 |
def load_flan():
|
| 49 |
+
tok = AutoTokenizer.from_pretrained("google/flan-t5-base")
|
| 50 |
+
mod = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
| 51 |
+
return tok, mod
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
@st.cache_resource
|
| 54 |
+
def load_deepseek():
|
| 55 |
+
tok = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct")
|
| 56 |
+
mod = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct")
|
| 57 |
+
return tok, mod
|
|
|
|
| 58 |
|
| 59 |
+
# Run selected model logic
|
| 60 |
if user_input:
|
| 61 |
with st.spinner("Generating response..."):
|
| 62 |
+
if st.session_state.mode == "Code Generator":
|
|
|
|
|
|
|
| 63 |
tok, mod = load_codegen()
|
| 64 |
+
inputs = tok(user_input, return_tensors="pt")
|
| 65 |
+
outputs = mod.generate(
|
| 66 |
+
inputs.input_ids,
|
| 67 |
+
max_new_tokens=128,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
temperature=0.7,
|
| 70 |
+
pad_token_id=tok.eos_token_id
|
| 71 |
+
)
|
| 72 |
+
res = tok.decode(outputs[0], skip_special_tokens=True)
|
| 73 |
+
st.subheader("π» Code Output")
|
| 74 |
+
st.code(res, language="python")
|
| 75 |
+
|
| 76 |
+
elif st.session_state.mode == "General Q&A":
|
| 77 |
+
tok, mod = load_flan()
|
| 78 |
+
inputs = tok(user_input, return_tensors="pt")
|
| 79 |
+
outputs = mod.generate(inputs.input_ids, max_new_tokens=100)
|
| 80 |
+
res = tok.decode(outputs[0], skip_special_tokens=True)
|
| 81 |
+
st.subheader("π Answer")
|
| 82 |
+
st.write(res)
|
| 83 |
|
| 84 |
+
elif st.session_state.mode == "Bayesian Q&A":
|
| 85 |
tok, mod = load_flan()
|
| 86 |
+
inputs = tok(user_input, return_tensors="pt")
|
| 87 |
+
outputs = mod.generate(
|
| 88 |
+
inputs.input_ids,
|
| 89 |
+
do_sample=True,
|
| 90 |
+
top_k=50,
|
| 91 |
+
temperature=0.9,
|
| 92 |
+
max_new_tokens=100
|
| 93 |
+
)
|
| 94 |
+
res = tok.decode(outputs[0], skip_special_tokens=True)
|
| 95 |
+
st.subheader("π Bayesian-style Sampled Answer")
|
| 96 |
+
st.write(res)
|
| 97 |
+
|
| 98 |
+
elif st.session_state.mode == "DeepSeek":
|
| 99 |
+
tok, mod = load_deepseek()
|
| 100 |
+
inputs = tok(user_input, return_tensors="pt")
|
| 101 |
+
outputs = mod.generate(
|
| 102 |
+
inputs.input_ids,
|
| 103 |
+
max_new_tokens=128,
|
| 104 |
+
temperature=0.7,
|
| 105 |
+
do_sample=True,
|
| 106 |
+
pad_token_id=tok.eos_token_id
|
| 107 |
+
)
|
| 108 |
+
res = tok.decode(outputs[0], skip_special_tokens=True)
|
| 109 |
+
st.subheader("π DeepSeek Coder Output")
|
| 110 |
+
st.code(res, language="python")
|
| 111 |
+
|
| 112 |
+
elif st.session_state.mode == "Tokenizer":
|
| 113 |
+
tok, _ = load_flan()
|
| 114 |
+
tokens = tok.tokenize(user_input)
|
| 115 |
+
ids = tok.convert_tokens_to_ids(tokens)
|
| 116 |
+
st.subheader("π Tokenization Breakdown")
|
| 117 |
+
st.write({"Tokens": tokens, "Token IDs": ids})
|