Spaces:
Sleeping
Sleeping
Commit
·
90291f3
1
Parent(s):
7c6ce23
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from langchain.chains import create_extraction_chain
|
| 4 |
+
from langchain.chains import create_tagging_chain
|
| 5 |
+
from langchain.chat_models import ChatOpenAI
|
| 6 |
+
from langchain.prompts import ChatPromptTemplate
|
| 7 |
+
from langchain.schema.output_parser import StrOutputParser
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def process_inputs(input_text, schema_prompt, radio_choice):
|
| 11 |
+
if radio_choice == "Extraction":
|
| 12 |
+
return process_extract(input_text, schema_prompt)
|
| 13 |
+
elif radio_choice == "Tagging":
|
| 14 |
+
return process_tag(input_text, schema_prompt)
|
| 15 |
+
else:
|
| 16 |
+
return process_custom(input_text, schema_prompt)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def process_extract(input_text, schema_prompt):
|
| 20 |
+
schema_json = json.loads(schema_prompt)
|
| 21 |
+
chain = create_extraction_chain(schema_json, chat_model)
|
| 22 |
+
llm_response = chain.run(input_text)
|
| 23 |
+
pretty_json_string = json.dumps(llm_response, indent=4)
|
| 24 |
+
return pretty_json_string
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def process_tag(input_text, schema_prompt):
|
| 28 |
+
schema_json = json.loads(schema_prompt)
|
| 29 |
+
chain = create_tagging_chain(schema_json, chat_model)
|
| 30 |
+
llm_response = chain.run(input_text)
|
| 31 |
+
pretty_json_string = json.dumps(llm_response, indent=4)
|
| 32 |
+
return pretty_json_string
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def process_custom(input_text, schema_prompt):
|
| 36 |
+
prompt = ChatPromptTemplate.from_template(schema_prompt)
|
| 37 |
+
output_parser = StrOutputParser()
|
| 38 |
+
chain = prompt | chat_model | output_parser
|
| 39 |
+
invocation_dict = {"input_text": input_text}
|
| 40 |
+
llm_response = chain.invoke(invocation_dict)
|
| 41 |
+
pretty_json_string = json.dumps(json.loads(llm_response), indent=4)
|
| 42 |
+
return pretty_json_string
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
chat_model = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo')
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
input_text = gr.Textbox(label="Input Text")
|
| 49 |
+
schema_prompt = gr.Textbox(label="Schema / Prompt")
|
| 50 |
+
radio_choice = gr.Radio(["Extraction", "Tagging", "Custom Prompt"], label="Task")
|
| 51 |
+
output = gr.Textbox(label="Result")
|
| 52 |
+
analyze_btn = gr.Button("Analyze")
|
| 53 |
+
analyze_btn.click(fn=process_inputs, inputs=[input_text, schema_prompt, radio_choice], outputs=output,
|
| 54 |
+
api_name="process_inputs")
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch(show_api=False, debug=True, share=True)
|