import os import gradio as gr from openai import OpenAI openai_client = OpenAI(api_key=os.getenv('OPENAI_APIKEY')) def get_gpt_representation(title: str, ph: list, model: str): system_prompt = f""" 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. Exact Match -> This method is used when the pharses have some words which need to excatly match with the utterance to identify that intent Embeddings -> This method is used when contextual information is essential to identify that intent. Your response should be either "Exact Match" or "Embeddings" """ user_prompt = f"""intent: {title}\nphrases: {ph}""" response = openai_client.chat.completions.create( model=model, messages=[ { "role": "system", "content": system_prompt, }, { "role": "user", "content": user_prompt }, ], temperature=0.5, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) cat = response.choices[0].message.content return cat.strip() # Store the phrases dynamically phrases = [] # Function to add new textboxes and process phrases def add_phrase(title,phrase): global phrases if phrase: # Only add if the phrase is not empty phrases.append(phrase) if len(phrases) >= 5: answer = get_gpt_representation(title,phrases,model='gpt-4o') if answer.lower() == 'embeddings': suggestion = "This Intent is perfectly suited for Semantic Matching , Well done on the phrases" else: suggestion = "This intent is more suited for Exact Matching than Semantic Matching" else: # Dummy suggestion logic (you can replace it with your own) suggestion = f"Please Enter Atleast 5 Phrases for the given intent , current number of phrases are {len(phrases)}" return phrases, suggestion # Function to clear the phrases and reset def reset(): global phrases phrases = [] return [], "" # Create Gradio components with gr.Blocks() as app: gr.Markdown( """
Add an Intent And its contributing Phrases
""", elem_id="title" ) intent_title = gr.Textbox(label="Intent",placeholder="Enter Intent Name") phrase_box = gr.Textbox(label="Phrase", placeholder="Enter a phrase") add_button = gr.Button("Add Phrase") phrase_list = gr.Textbox(label="Added Phrases", interactive=False) suggestion_box = gr.Textbox(label="Suggestion", interactive=False) reset_button = gr.Button("Reset") # When 'Add Phrase' is clicked, the add_phrase function will be called add_button.click(add_phrase, inputs=[intent_title,phrase_box], outputs=[phrase_list, suggestion_box]) # Reset button to clear phrases reset_button.click(reset, inputs=None, outputs=[phrase_list, suggestion_box]) # Launch the app app.launch()