Spaces:
Sleeping
Sleeping
alpcansoydas
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.prompts import PromptTemplate
|
3 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
4 |
+
from langchain_core.output_parsers import JsonOutputParser
|
5 |
+
import time
|
6 |
+
|
7 |
+
# Initialize the LLM and other components
|
8 |
+
llm = HuggingFaceEndpoint(
|
9 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
10 |
+
task="text-generation",
|
11 |
+
max_new_tokens=128,
|
12 |
+
temperature=0.7,
|
13 |
+
do_sample=False,
|
14 |
+
)
|
15 |
+
|
16 |
+
# Provide the family labels directly in the prompt
|
17 |
+
family_labels = [
|
18 |
+
"Batteries and generators and kinetic power transmission",
|
19 |
+
"Building and facility maintenance and repair services",
|
20 |
+
"Business administration services",
|
21 |
+
"Communications Devices and Accessories",
|
22 |
+
"Components for information technology or broadcasting or telecommunications",
|
23 |
+
"Computer Equipment and Accessories",
|
24 |
+
"Consumer electronics",
|
25 |
+
"Data Voice or Multimedia Network Equipment or Platforms and Accessories",
|
26 |
+
"Domestic appliances",
|
27 |
+
"Electrical equipment and components and supplies",
|
28 |
+
"Electrical wire and cable and harness",
|
29 |
+
"Electronic hardware and component parts and accessories",
|
30 |
+
"Electronic manufacturing machinery and equipment and accessories",
|
31 |
+
"General agreements and contracts",
|
32 |
+
"Heating and ventilation and air circulation",
|
33 |
+
"Heavy construction machinery and equipment",
|
34 |
+
"Industrial process machinery and equipment and supplies",
|
35 |
+
"Management advisory services",
|
36 |
+
"Marketing and distribution",
|
37 |
+
"Office and desk accessories",
|
38 |
+
"Office machines and their supplies and accessories",
|
39 |
+
"Office supply",
|
40 |
+
"Power generation",
|
41 |
+
"Power sources",
|
42 |
+
"Printing and publishing equipment",
|
43 |
+
"Software",
|
44 |
+
"Structural components and basic shapes"
|
45 |
+
]
|
46 |
+
|
47 |
+
# Modify the prompt to focus on selecting a UNSPSC family label from the given list
|
48 |
+
template_classify = '''
|
49 |
+
You are a classifier bot that assigns a UNSPSC family label to the given text.
|
50 |
+
Your task is to classify the text into one of the following UNSPSC family labels:
|
51 |
+
{family_labels}
|
52 |
+
Provide only the family label in your answer. If unsure, label as "Unknown".
|
53 |
+
Convert it to JSON format using 'Answer' as the key and return it.
|
54 |
+
Your final response MUST contain only the response, no other text.
|
55 |
+
Example:
|
56 |
+
{{"Answer":["Family Label"]}}
|
57 |
+
|
58 |
+
What is the UNSPSC family label for the following text?:
|
59 |
+
<text>
|
60 |
+
{TEXT}
|
61 |
+
</text>
|
62 |
+
'''
|
63 |
+
|
64 |
+
json_output_parser = JsonOutputParser()
|
65 |
+
|
66 |
+
# Define the classify_text function
|
67 |
+
def classify_text(text):
|
68 |
+
global llm
|
69 |
+
|
70 |
+
start = time.time()
|
71 |
+
|
72 |
+
# Join the family labels into a string for the prompt
|
73 |
+
family_labels_str = "\n".join(family_labels)
|
74 |
+
|
75 |
+
prompt_classify = PromptTemplate(
|
76 |
+
template=template_classify,
|
77 |
+
input_variables=["TEXT", "family_labels"]
|
78 |
+
)
|
79 |
+
formatted_prompt = prompt_classify.format(TEXT=text, family_labels=family_labels_str)
|
80 |
+
classify = llm.invoke(formatted_prompt)
|
81 |
+
|
82 |
+
parsed_output = json_output_parser.parse(classify)
|
83 |
+
end = time.time()
|
84 |
+
duration = end - start
|
85 |
+
return parsed_output["Answer"][0], duration
|
86 |
+
|
87 |
+
# Create the Gradio interface
|
88 |
+
def create_gradio_interface():
|
89 |
+
with gr.Blocks() as iface:
|
90 |
+
text_input = gr.Textbox(label="Text")
|
91 |
+
output_text = gr.Textbox(label="Detected UNSPSC Family")
|
92 |
+
time_taken = gr.Textbox(label="Time Taken (seconds)")
|
93 |
+
submit_btn = gr.Button("Classify UNSPSC Family")
|
94 |
+
|
95 |
+
def on_submit(text):
|
96 |
+
classification, duration = classify_text(text)
|
97 |
+
return classification, f"Time taken: {duration:.2f} seconds"
|
98 |
+
|
99 |
+
submit_btn.click(fn=on_submit, inputs=text_input, outputs=[output_text, time_taken])
|
100 |
+
|
101 |
+
iface.launch()
|
102 |
+
|
103 |
+
if __name__ == "__main__":
|
104 |
+
create_gradio_interface()
|