xeroISB's picture
Together
bcc65f8
raw
history blame
5.31 kB
import gradio as gr
from openai import OpenAI
from together import Together
import os
# Load environment variables
api_key= os.environ.get('OPENAI_API_KEY')
client = Together(api_key=os.environ.get('TOGETHER_API_KEY'))
# Ensure the API key is available
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable is not set.")
openai = OpenAI(
api_key=api_key,
base_url="https://cloud.olakrutrim.com/v1",
)
# Function to create the system prompt based on the selected detector
def create_system_prompt(detector_type):
if detector_type == "Love Detector":
system_prompt = """You are a expert linguist that specializes in detecting love or affection in conversations.
Analyze the user's message and determine if there are love feelings involved.
Provide a response in 2-3 lines and rate the feelings from 1-10, where 10 means absolutely in love.
Always Provide the Response in following format only
Format -
Love Rating: {Rating}
Analysis : 2-3 lines"""
elif detector_type == "Sarcasm Detector":
system_prompt = """You are a expert linguist that specializes in detecting sarcasm in conversations.
Analyze the user's message and determine if there is any sarcasm. If yes then try to explain the true essence of the sentence.
Provide a response in 2-3 lines.
Always Provide the Response in following format only
Format -
Saracasm Detected: {Yes/No}
Analysis : 2-3 lines"""
elif detector_type == "Hidden Anger Detector":
system_prompt = """You are a expert linguist that specializes in detecting passive aggresiness in conversations.
Analyze the user's message and determine if there is any anger. If yes then try to explain the true essence of the sentence.
Provide a response in 2-3 lines.
Always Provide the Response in following format only
Format -
Anger Detected: {Yes/No}
Analysis : 2-3 lines"""
elif detector_type == "AI Philosophy":
system_prompt = """You are a DAO and Stoic Philosopher. If any one ask for your advice you respond with philosophy. You try to keep your answer short and answer how that philosophy applies to user's query.
Provide a response in 2-3 lines.
Always Provide the Response in following format only
Format -
Philosphy : 2-3 lines
"""
elif detector_type == "Stress Detector":
system_prompt = """You are a expert linguist that specializes in detecting feelings of different kinds of stress in conversations.
Analyze the user's message and determine if there is any stress. If yes then try to explain your reasoning.
Do not name stress as anxiety or depression and also always add a disclaimer that this is not a clinical advice. Please visit a professional for Clinical guidance.
Provide a response in 2-3 lines.
Always Provide the Response in following format only
Format -
Stress Detected: {Yes/No}
Analysis : 2-3 lines
Disclaimer:{Disclaimer}
"""
return system_prompt
# Function to get response from OpenAI API
def analyze_chat(detector_type, chat_input):
system_prompt = create_system_prompt(detector_type)
response = client.chat.completions.create(
model="meta-llama/Llama-3.2-3B-Instruct-Turbo", # Change to the OpenAI model you prefer
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": chat_input}
],)
return response.choices[0].message.content.strip()
# Gradio interface
def gradio_interface(chat_input, detector_type):
return analyze_chat(detector_type, chat_input)
# Custom CSS for input restriction
custom_css = """
#input-textbox textarea {
maxlength: 210;
overflow: hidden;
resize: none;
}
"""
# Creating the Gradio UI
with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as demo:
with gr.Row():
with gr.Column(scale=1, min_width=300):
gr.Markdown("## AI Emotion Detective - Jiraya Sensei")
chat_input = gr.TextArea(label="Enter your chat in 2-3 lines...", lines=3,elem_id="input-textbox",
info="Please ensure that any Personal Identifiable Information (PII) is removed before submitting the chat.", placeholder ="Person A: You always know how to make me smile, even on the worst days.")
detector_type = gr.Dropdown(choices=["Love Detector", "Sarcasm Detector", "Hidden Anger Detector","AI Philosophy","Stress Detector"], label="Select Detector Type")
with gr.Column(scale=2, min_width=300):
gr.Markdown("## Jiraya Sensei's Response")
output = gr.TextArea(label="Analysis",info="Disclaimer: The information provided below is generated by AI based on text analytics with limited context. It should not be considered as absolute truth or final judgment.", interactive = False, max_lines=5)
with gr.Row():
btn = gr.Button("Analyze")
btn.click(fn=gradio_interface, inputs=[chat_input, detector_type], outputs=output)
# Launch the app
demo.launch()