Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
2 |
+
from peft import prepare_model_for_kbit_training
|
3 |
+
from peft import LoraConfig, get_peft_model
|
4 |
+
from datasets import load_dataset
|
5 |
+
import transformers
|
6 |
+
|
7 |
+
# load model from hub
|
8 |
+
from peft import PeftModel, PeftConfig
|
9 |
+
from transformers import AutoModelForCausalLM
|
10 |
+
|
11 |
+
model_name = "TheBloke/Mistral-7B-Instruct-v0.2-GPTQ"
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name,
|
13 |
+
device_map="auto",
|
14 |
+
trust_remote_code=False,
|
15 |
+
revision="main")
|
16 |
+
|
17 |
+
config = PeftConfig.from_pretrained("saanvi-bot/jayson")
|
18 |
+
model = PeftModel.from_pretrained(model, "saanvi-bot/jayson")
|
19 |
+
|
20 |
+
# load tokenizer
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
22 |
+
|
23 |
+
"""### Use Fine-tuned Model"""
|
24 |
+
|
25 |
+
intstructions_string = f"""convert into json format \n"""
|
26 |
+
|
27 |
+
prompt_template = lambda comment: f'''[INST] {intstructions_string} \n{comment} \n[/INST]'''
|
28 |
+
|
29 |
+
model.eval()
|
30 |
+
|
31 |
+
# Streamlit interface
|
32 |
+
st.title("Text to JSON Converter")
|
33 |
+
st.write("Enter the text you want to convert to JSON format:")
|
34 |
+
|
35 |
+
# Text input from the user
|
36 |
+
user_input = st.text_area("Input text", height=200)
|
37 |
+
|
38 |
+
# Convert input text to JSON
|
39 |
+
if st.button("Convert"):
|
40 |
+
prompt = prompt_template(user_input)
|
41 |
+
|
42 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
43 |
+
outputs = model.generate(input_ids=inputs["input_ids"], max_new_tokens=200)
|
44 |
+
|
45 |
+
json_output = tokenizer.batch_decode(outputs)[0]
|
46 |
+
st.json(json_output)
|