Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from unsloth import FastLanguageModel
|
3 |
+
from transformers import TextStreamer
|
4 |
+
|
5 |
+
# Page Configuration
|
6 |
+
st.set_page_config(page_title="AI Traffic Law Advisor", layout="wide")
|
7 |
+
|
8 |
+
# Load the LoRA model
|
9 |
+
MODEL_PATH = "./lora_model/lora_model"
|
10 |
+
|
11 |
+
@st.cache_resource(show_spinner=False)
|
12 |
+
def load_model():
|
13 |
+
# Load model and tokenizer
|
14 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
15 |
+
MODEL_PATH,
|
16 |
+
device_map="auto"
|
17 |
+
)
|
18 |
+
# Enable inference mode
|
19 |
+
model = FastLanguageModel.for_inference(model)
|
20 |
+
return model, tokenizer
|
21 |
+
|
22 |
+
model, tokenizer = load_model()
|
23 |
+
|
24 |
+
st.title("AI Traffic Law Advisor")
|
25 |
+
|
26 |
+
user_query = st.text_area("Enter your legal question about traffic rules in India:", "")
|
27 |
+
|
28 |
+
if st.button("Get Advice"):
|
29 |
+
if user_query.strip():
|
30 |
+
messages = [{"role": "user", "content": user_query}]
|
31 |
+
# Tokenize input
|
32 |
+
inputs = tokenizer.apply_chat_template(
|
33 |
+
messages,
|
34 |
+
tokenize=True,
|
35 |
+
add_generation_prompt=True,
|
36 |
+
return_tensors="pt"
|
37 |
+
).to(model.device)
|
38 |
+
|
39 |
+
# Stream response
|
40 |
+
text_streamer = TextStreamer(tokenizer, skip_prompt=True)
|
41 |
+
|
42 |
+
st.markdown("**AI Response:**")
|
43 |
+
with st.spinner("Generating response..."):
|
44 |
+
model.generate(input_ids=inputs, streamer=text_streamer, max_new_tokens=1048, temperature=0.7)
|
45 |
+
else:
|
46 |
+
st.warning("Please enter a query.")
|