Spaces:
Sleeping
Sleeping
prithivi96
commited on
Commit
•
1796c2b
1
Parent(s):
bfa01a4
Upload 3 files
Browse files- app.py +4 -21
- backend.py +47 -0
- frontend.py +40 -0
app.py
CHANGED
@@ -1,22 +1,5 @@
|
|
1 |
-
import
|
2 |
-
import streamlit as st
|
3 |
-
import tempfile
|
4 |
-
import pandas as pd
|
5 |
-
from JSONPath_Generator import JSONPath_Generator
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
load_dotenv()
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#Input Text Area
|
14 |
-
json_value = st.text_area(label="JSON Body Input: ")
|
15 |
-
target_input = st.text_input(label="Target JSON Key: ")
|
16 |
-
json_condition = st.text_input(label="JSON Conditions (If any): ")
|
17 |
-
|
18 |
-
if st.button("Submit"):
|
19 |
-
json_path_gen = JSONPath_Generator(json_input=json_value, target_value=target_input,
|
20 |
-
json_condition=json_condition)
|
21 |
-
res = json_path_gen.create_llm_chain()
|
22 |
-
st.write(res)
|
|
|
1 |
+
from frontend import JSONQuery_Frontend
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
if __name__ == "__main__":
|
4 |
+
app_frntend = JSONQuery_Frontend()
|
5 |
+
app_frntend.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.prompts import PromptTemplate, SystemMessagePromptTemplate, ChatPromptTemplate, \
|
2 |
+
HumanMessagePromptTemplate
|
3 |
+
from langchain.llms import OpenAI
|
4 |
+
from langchain.chat_models import ChatOpenAI, AzureChatOpenAI
|
5 |
+
from langchain.cache import InMemoryCache
|
6 |
+
import langchain
|
7 |
+
langchain.llm_cache = InMemoryCache()
|
8 |
+
import pandas as pd
|
9 |
+
import os
|
10 |
+
from langchain.chains import LLMChain
|
11 |
+
from langchain.output_parsers import PydanticOutputParser
|
12 |
+
from pydantic import BaseModel, Field
|
13 |
+
|
14 |
+
|
15 |
+
class JSONPath_Generator:
|
16 |
+
def __init__(self, json_input, target_value, json_condition, openai_key):
|
17 |
+
self.json_input = json_input
|
18 |
+
self.target_value = target_value
|
19 |
+
self.json_condition = json_condition
|
20 |
+
self.openai_api_key = openai_key
|
21 |
+
self.model = OpenAI(
|
22 |
+
temperature=0,
|
23 |
+
openai_api_key=self.openai_api_key,
|
24 |
+
model_name="gpt-3.5-turbo-instruct"
|
25 |
+
)
|
26 |
+
|
27 |
+
def create_chat_prompt(self):
|
28 |
+
# System Template
|
29 |
+
with open("system_template.txt", "r") as sys_temp:
|
30 |
+
system_template = sys_temp.read().strip()
|
31 |
+
system_prompt = SystemMessagePromptTemplate.from_template(system_template)
|
32 |
+
# Humman Template
|
33 |
+
with open("human_template.txt", "r") as hum_temp:
|
34 |
+
human_template = hum_temp.read().strip()
|
35 |
+
if self.json_condition != '':
|
36 |
+
human_template += " provided the {json_condition}"
|
37 |
+
human_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
38 |
+
# Chat Prompt
|
39 |
+
self.chat_prompt = ChatPromptTemplate.from_messages([system_prompt, human_prompt])
|
40 |
+
|
41 |
+
def create_llm_chain(self):
|
42 |
+
# self.read_extract_api_details()
|
43 |
+
self.create_chat_prompt()
|
44 |
+
chain = LLMChain(llm=self.model, prompt=self.chat_prompt)
|
45 |
+
self.response = chain.run(Target_value=self.target_value,json_condition = self.json_condition,
|
46 |
+
JSON_Input=self.json_input)
|
47 |
+
return self.response
|
frontend.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from backend import JSONPath_Generator
|
3 |
+
|
4 |
+
|
5 |
+
class JSONQuery_Frontend:
|
6 |
+
def __init__(self):
|
7 |
+
pass
|
8 |
+
|
9 |
+
def run(self):
|
10 |
+
self.titleheader()
|
11 |
+
self.input_text()
|
12 |
+
self.submit_btn()
|
13 |
+
|
14 |
+
def titleheader(self):
|
15 |
+
# Initialize Streamlit app
|
16 |
+
st.set_page_config(page_title="👨💻 JSON Path Generator")
|
17 |
+
st.header("JSON Path Generator")
|
18 |
+
|
19 |
+
|
20 |
+
def input_text(self):
|
21 |
+
# Input Text Area
|
22 |
+
self.open_ai_key = st.text_input(label="Enter the OpenAI Key: ")
|
23 |
+
self.json_value = st.text_area(label="JSON Body Input: ")
|
24 |
+
self.target_input = st.text_input(label="Target JSON Key: ")
|
25 |
+
self.json_condition = st.text_input(label="JSON Conditions (If any): ")
|
26 |
+
|
27 |
+
def submit_btn(self):
|
28 |
+
if st.button("Submit"):
|
29 |
+
self.json_path_gen = JSONPath_Generator(json_input=self.json_value, target_value=self.target_input,
|
30 |
+
json_condition=self.json_condition,
|
31 |
+
openai_key=self.open_ai_key)
|
32 |
+
res = self.json_path_gen.create_llm_chain()
|
33 |
+
st.write(res)
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|