lakshmikarpolam commited on
Commit
347c564
1 Parent(s): 18174b2
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import torch
3
+ from transformers import pipeline
4
+ import streamlit as st
5
+
6
+ # Load the text-generation pipeline
7
+ pipe = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-alpha", torch_dtype=torch.bfloat16, device_map="auto")
8
+
9
+ delimiter = "####"
10
+ system_message = f"""
11
+ You will be provided with user data. \
12
+ The user data will be delimited with \
13
+ {delimiter} characters.
14
+
15
+ Extract key information as shown in the examples shown below, to add an item in the ERPnext application. Give the output as a python dictionary object. Do not use information outside from what
16
+ is given inside the text to fill the values. Do not provide any explaination.
17
+
18
+ Example1:
19
+ prompt: "I had made an order for 6 Units of item Logitech G15. The platform said that there were 10 Units available in stock before i made my purchase, but now it shows that the item is out of stock even though I have made a payment of 6000 towards my order.",
20
+ "item_code": "None",
21
+ "item_name": "Logitech G15",
22
+ "item_group": "None",
23
+ "stock_uom": "Unit",
24
+ "description": "None",
25
+ "standard_rate": 1000
26
+
27
+ Example2:
28
+ prompt": "Hello, I have not received my order of 5Litres Saffola Gold oil. I made an order on 07-09-2023",
29
+ "item_code": "None",
30
+ "item_name": "Saffola Gold Oil",
31
+ "item_group": "None",
32
+ "stock_uom": "Litres",
33
+ "description": "None",
34
+ "standard_rate": "None"
35
+
36
+ Example3:
37
+ prompt": "Please add an entry of a new item in our inventory, Code IB707, and name i-ball Keyboard K-5. Current stock includes 5000 Nos of the item, grouped under Products. Price per unit is Rs. 1500.",
38
+ "item_code": "IB707",
39
+ "item_name": "i-ball Keyboard K-5",
40
+ "item_group": "Product",
41
+ "stock_uom": "Nos",
42
+ "description": "None",
43
+ "standard_rate": 1500
44
+
45
+ """
46
+
47
+ # Create a Streamlit app
48
+ def main():
49
+ st.title("Prompt to JSON Tool")
50
+
51
+ st.write("This tool generates JSON output based on the user's prompt.")
52
+
53
+ user_input = st.text_area("Enter your prompt here")
54
+
55
+ if st.button("Generate JSON"):
56
+ messages = [
57
+ {"role": "system", "content": system_message},
58
+ {"role": "user", "content": f"{delimiter}{user_input}{delimiter}"},
59
+ ]
60
+
61
+ prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
62
+ outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
63
+ output_text = outputs[0]["generated_text"]
64
+
65
+ # Parse the relevant JSON information from the output text
66
+ json_output = output_text.split("Example1:")[-1].strip()
67
+
68
+ # Display the JSON output on the Streamlit app
69
+ st.write("JSON Output:")
70
+ st.write(json_output)
71
+
72
+ # Save the JSON output to a file
73
+ with open("output.json", "w") as f:
74
+ json.dump(json_output, f, indent=4)
75
+ st.write("JSON output saved to 'output.json'.")
76
+
77
+ if __name__ == "__main__":
78
+ main()