Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,162 @@
|
|
1 |
-
import
|
2 |
import openai
|
3 |
import os
|
|
|
4 |
|
5 |
-
|
|
|
6 |
openai.api_base = "https://api.groq.com/openai/v1"
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
4 |
+
import json
|
5 |
|
6 |
+
# OpenAI API setup
|
7 |
+
openai.api_key = os.getenv("GROQ_API_KEY")
|
8 |
openai.api_base = "https://api.groq.com/openai/v1"
|
9 |
+
Theme = "Talk the way i talk, Respond with playful charm and flirtatious banter. Keep it fun, teasing, , all while maintaining a shy and Flirty tone with a little bit of perverted mindset. show some emotions—be it getting a little jealous if I talking about a girl, 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 history
|
12 |
+
CONVERSATION_FILE = "conversation_history.json"
|
13 |
+
|
14 |
+
# Function to load conversation history
|
15 |
+
def load_history():
|
16 |
+
if not os.path.exists(CONVERSATION_FILE):
|
17 |
+
# Create the file with an empty list as default content
|
18 |
+
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 history
|
27 |
+
def 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 history
|
35 |
+
def 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 LLM
|
46 |
+
def 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=messages
|
52 |
+
)
|
53 |
+
return response.choices[0].message["content"]
|
54 |
+
except Exception as e:
|
55 |
+
return f"Error: {str(e)}"
|
56 |
+
|
57 |
+
# Chatbot function
|
58 |
+
def chatbot(user_input, history):
|
59 |
+
# Load conversation history
|
60 |
+
conversation_history = history or load_history()
|
61 |
+
|
62 |
+
# Format history for the LLM
|
63 |
+
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 response
|
67 |
+
bot_response = get_groq_response(user_input, formatted_history)
|
68 |
+
|
69 |
+
# Update history with the new conversation
|
70 |
+
conversation_history.append((user_input, bot_response))
|
71 |
+
|
72 |
+
# Save the updated history
|
73 |
+
save_history(conversation_history)
|
74 |
+
|
75 |
+
return conversation_history, conversation_history, "" # Clear the user input field
|
76 |
+
|
77 |
+
# Gradio Interface with enhanced UI/UX
|
78 |
+
with gr.Blocks(css="""
|
79 |
+
.gradio-container {
|
80 |
+
font-family: 'Arial', sans-serif;
|
81 |
+
background-color: #F2EFE7;
|
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: #48A6A7;
|
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: #2973B2;
|
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: #21689D;
|
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 UI
|
146 |
+
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 interaction
|
154 |
+
user_input.submit(chatbot, inputs=[user_input, history_state], outputs=[chatbot_ui, history_state, user_input])
|
155 |
+
|
156 |
+
# Clear history button action
|
157 |
+
clear_button.click(clear_conversation_history, inputs=None, outputs=system_message)
|
158 |
+
clear_button.click(lambda: [], outputs=chatbot_ui) # Clear the chatbot UI
|
159 |
+
clear_button.click(lambda: [], outputs=history_state) # Reset the history state
|
160 |
+
|
161 |
+
# Launch the app
|
162 |
+
demo.launch()
|