Spaces:
Running
Running
AIModels24
commited on
Add a app.py file
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
3 |
+
|
4 |
+
# Load the model and tokenizer
|
5 |
+
def load_model_and_tokenizer():
|
6 |
+
model_name = "AIModels24/Indian_Constitution" # Replace with your model name
|
7 |
+
|
8 |
+
# Define quantization configuration for 4-bit quantization
|
9 |
+
quant_config = BitsAndBytesConfig(load_in_4bit=True) # 4-bit quantization
|
10 |
+
|
11 |
+
# Load the tokenizer
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
+
|
14 |
+
# Load the model with 4-bit quantization
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
16 |
+
model_name,
|
17 |
+
quantization_config=quant_config,
|
18 |
+
device_map="auto" # Automatically use available devices (GPU/CPU)
|
19 |
+
)
|
20 |
+
|
21 |
+
return model, tokenizer
|
22 |
+
|
23 |
+
# Load model and tokenizer using the function
|
24 |
+
model, tokenizer = load_model_and_tokenizer()
|
25 |
+
|
26 |
+
## prompt function
|
27 |
+
alpaca_prompt = "### Instruction:\n{}\n\n### Response:\n"
|
28 |
+
|
29 |
+
|
30 |
+
# Streamlit User Interface
|
31 |
+
st.title("भारतीय कानून व्यवस्था")
|
32 |
+
st.subheader("AI-powered responses for legal questions in Indian law")
|
33 |
+
|
34 |
+
# Input text box for user question
|
35 |
+
instruction = st.text_area("Enter your question:", placeholder="Ask a question about Indian law...")
|
36 |
+
|
37 |
+
# Generate response button
|
38 |
+
if st.button("Generate Response"):
|
39 |
+
if instruction.strip():
|
40 |
+
with st.spinner("Generating response..."):
|
41 |
+
# Prepare the prompt for the model
|
42 |
+
inputs = tokenizer(
|
43 |
+
[alpaca_prompt.format(instruction)],
|
44 |
+
return_tensors="pt"
|
45 |
+
).to("cuda")
|
46 |
+
|
47 |
+
# Generate the response
|
48 |
+
outputs = model.generate(**inputs, max_new_tokens=150, use_cache=True)
|
49 |
+
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
50 |
+
|
51 |
+
# Extract the clean response
|
52 |
+
response_cleaned = response.split("### Response:\n")[-1].strip()
|
53 |
+
|
54 |
+
# Display the response
|
55 |
+
st.success("Response:")
|
56 |
+
st.write(response_cleaned)
|
57 |
+
else:
|
58 |
+
st.error("Please enter a question to generate a response.")
|