Spaces:
Sleeping
Sleeping
Tanul Singh
commited on
Commit
·
7dcfabd
1
Parent(s):
16582d5
App
Browse files- README.md +5 -5
- app.py +92 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title: Intent
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: other
|
|
|
1 |
---
|
2 |
+
title: Intent Suggestions Demo
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.42.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: other
|
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from openai import OpenAI
|
4 |
+
|
5 |
+
openai_client = OpenAI(api_key=os.getenv('OPENAI_APIKEY'))
|
6 |
+
|
7 |
+
def get_gpt_representation(title: str, ph: list, model: str):
|
8 |
+
system_prompt = f"""
|
9 |
+
You are given the intent and phrases associated with that intent. Your task is to identify which method either Exact Match or Embeddings should be used to find the utterances similar to given phrases.
|
10 |
+
Exact Match -> This method is used when the pharses have some words which need to excatly match with the utterance to identify that intent
|
11 |
+
Embeddings -> This method is used when contextual information is essential to identify that intent.
|
12 |
+
Your response should be either "Exact Match" or "Embeddings"
|
13 |
+
"""
|
14 |
+
|
15 |
+
user_prompt = f"""intent: {title}\nphrases: {ph}"""
|
16 |
+
|
17 |
+
response = openai_client.chat.completions.create(
|
18 |
+
model=model,
|
19 |
+
messages=[
|
20 |
+
{
|
21 |
+
"role": "system",
|
22 |
+
"content": system_prompt,
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"role": "user",
|
26 |
+
"content": user_prompt
|
27 |
+
},
|
28 |
+
],
|
29 |
+
temperature=0.5,
|
30 |
+
max_tokens=256,
|
31 |
+
top_p=1,
|
32 |
+
frequency_penalty=0,
|
33 |
+
presence_penalty=0
|
34 |
+
)
|
35 |
+
cat = response.choices[0].message.content
|
36 |
+
return cat.strip()
|
37 |
+
|
38 |
+
|
39 |
+
# Store the phrases dynamically
|
40 |
+
phrases = []
|
41 |
+
|
42 |
+
# Function to add new textboxes and process phrases
|
43 |
+
def add_phrase(title,phrase):
|
44 |
+
global phrases
|
45 |
+
if phrase: # Only add if the phrase is not empty
|
46 |
+
phrases.append(phrase)
|
47 |
+
|
48 |
+
if len(phrases) >= 5:
|
49 |
+
answer = get_gpt_representation(title,phrases,model='gpt-4o')
|
50 |
+
if answer.lower() == 'embedding':
|
51 |
+
suggestion = "This Intent is perfectly suited for Semantic Matching , Well done on the phrases"
|
52 |
+
else:
|
53 |
+
suggestion = "This intent is more suited for Exact Matching than Semantic Matching"
|
54 |
+
else:
|
55 |
+
# Dummy suggestion logic (you can replace it with your own)
|
56 |
+
suggestion = f"Please Enter Atleast 5 Phrases for the given intent , current number of phrases are {len(phrases)}"
|
57 |
+
|
58 |
+
return phrases, suggestion
|
59 |
+
|
60 |
+
# Function to clear the phrases and reset
|
61 |
+
def reset():
|
62 |
+
global phrases
|
63 |
+
phrases = []
|
64 |
+
return [], ""
|
65 |
+
|
66 |
+
|
67 |
+
# Create Gradio components
|
68 |
+
with gr.Blocks() as app:
|
69 |
+
gr.Markdown(
|
70 |
+
"""
|
71 |
+
<h1 style='text-align: center; color: #4CAF50;'>Intent Suggestions</h1>
|
72 |
+
<p style='text-align: center;'>Add an Intent And its contributing Phrases</p>
|
73 |
+
""",
|
74 |
+
elem_id="title"
|
75 |
+
)
|
76 |
+
|
77 |
+
intent_title = gr.Textbox(label="Intent",placeholder="Enter Intent Name")
|
78 |
+
phrase_box = gr.Textbox(label="Phrase", placeholder="Enter a phrase")
|
79 |
+
add_button = gr.Button("Add Phrase")
|
80 |
+
|
81 |
+
phrase_list = gr.Textbox(label="Added Phrases", interactive=False)
|
82 |
+
suggestion_box = gr.Textbox(label="Suggestion", interactive=False)
|
83 |
+
reset_button = gr.Button("Reset")
|
84 |
+
|
85 |
+
# When 'Add Phrase' is clicked, the add_phrase function will be called
|
86 |
+
add_button.click(add_phrase, inputs=[intent_title,phrase_box], outputs=[phrase_list, suggestion_box])
|
87 |
+
|
88 |
+
# Reset button to clear phrases
|
89 |
+
reset_button.click(reset, inputs=None, outputs=[phrase_list, suggestion_box])
|
90 |
+
|
91 |
+
# Launch the app
|
92 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gradio
|