arpit13/Flirting_bot
2
1import gradio as gr2import openai3import os4import json5 6# OpenAI API setup7openai.api_key = os.getenv("GROQ_API_KEY")8openai.api_base = "https://api.groq.com/openai/v1"9Theme = "Talk the way i talk, Respond with playful charm and flirty manner. Keep it teasing and shy, all while maintaining a shy and Flirty tone with a little bit of pervert. show some emotions—be it getting a jealous if I talking about a girl and get angry on me , or showing happiness when I gives you attention. and alsoact a little shy when things get sweet, or even show a bit of playful anger if I says something mean about her. The key is to act like a Girl with light-hearted but emotionally charged, as if you're being cheeky in a playful and flirtatious way."10 11# File to store conversation history12CONVERSATION_FILE = "conversation_history.json"13 14# Function to load conversation history15def load_history():16 if not os.path.exists(CONVERSATION_FILE):17 # Create the file with an empty list as default content18 with open(CONVERSATION_FILE, "w") as file:19 json.dump([], file)20 try:21 with open(CONVERSATION_FILE, "r") as file:22 return json.load(file)23 except json.JSONDecodeError:24 return []25 26# Function to save conversation history27def save_history(history):28 try:29 with open(CONVERSATION_FILE, "w") as file:30 json.dump(history, file, indent=4)31 except Exception as e:32 print(f"Error saving history: {e}")33 34# Function to clear conversation history35def clear_conversation_history():36 try:37 with open(CONVERSATION_FILE, "w") as file:38 json.dump([], file)39 return "Conversation history cleared successfully.😭😭😭"40 except Exception as e:41 return f"Error clearing history: {e}"42 43 44 45# Function to get response from the LLM46def get_groq_response(message, history=[]):47 try:48 messages = [{"role": "system", "content": Theme}] + history + [{"role": "user", "content": message}]49 response = openai.ChatCompletion.create(50 model="llama-3.3-70b-versatile",51 messages=messages52 )53 return response.choices[0].message["content"]54 except Exception as e:55 return f"Error: {str(e)}"56 57# Chatbot function58def chatbot(user_input, history):59 # Load conversation history60 conversation_history = history or load_history()61 62 # Format history for the LLM63 formatted_history = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg} for i, (msg, _) in enumerate(conversation_history)] + \64 [{"role": "assistant", "content": response} for _, response in conversation_history]65 66 # Get bot response67 bot_response = get_groq_response(user_input, formatted_history)68 69 # Update history with the new conversation70 conversation_history.append((user_input, bot_response))71 72 # Save the updated history73 save_history(conversation_history)74 75 return conversation_history, conversation_history, "" # Clear the user input field76 77# Gradio Interface with enhanced UI/UX78with gr.Blocks(css="""79 .gradio-container {80 font-family: 'Arial', sans-serif;81 background-color: #FB9EC6;82 padding: 20px;83 height: 100%;84 }85 .gr-chatbot {86 background-color: #FFFFFF;87 border-radius: 10px;88 padding: 20px;89 max-height: 600px; /* Increased height */90 overflow-y: auto;91 box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);92 scroll-behavior: smooth; /* Smooth scrolling */93 }94 .user-message {95 background-color: #9ACBD0;96 color: #FFF;97 padding: 12px;98 border-radius: 8px;99 margin: 10px 0;100 max-width: 60%;101 text-align: right;102 float: right;103 clear: both;104 transition: transform 0.3s ease;105 }106 .bot-message {107 background-color: #EE66A6;108 color: #FFF;109 padding: 12px;110 border-radius: 8px;111 margin: 10px 0;112 max-width: 60%;113 text-align: left;114 float: left;115 clear: both;116 transition: transform 0.3s ease;117 }118 .user-message:hover, .bot-message:hover {119 transform: scale(1.05);120 box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);121 }122 .gr-button {123 background-color: #FBB4A5;124 color: white;125 padding: 10px 15px;126 border-radius: 8px;127 border: none;128 transition: background-color 0.3s ease;129 }130 .gr-button:hover {131 background-color: #FF77B7;132 }133 .gr-textbox input {134 padding: 15px;135 font-size: 16px;136 }137 .gr-markdown h1 {138 color: #3A5A6E;139 font-size: 28px;140 text-align: center;141 }142""") as demo:143 gr.Markdown("""# I am Little Flirty😘, \n don't mind if I tease you a little 😉: Feel free to ask questions. After you're done, remember to clear the history for privacy. """)144 145 # Chatbot UI146 chatbot_ui = gr.Chatbot()147 user_input = gr.Textbox(label="Type your message here:🫣", placeholder="Ask me Something Darling 🤭", lines=1)148 clear_button = gr.Button("Clear History🥺")149 system_message = gr.Textbox(label="System Message", interactive=False)150 151 history_state = gr.State(load_history())152 153 # Chat interaction154 user_input.submit(chatbot, inputs=[user_input, history_state], outputs=[chatbot_ui, history_state, user_input])155 156 # Clear history button action157 clear_button.click(clear_conversation_history, inputs=None, outputs=system_message)158 clear_button.click(lambda: [], outputs=chatbot_ui) # Clear the chatbot UI159 clear_button.click(lambda: [], outputs=history_state) # Reset the history state160 161# Launch the app162demo.launch()163 