Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,188 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
-
demo
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
from typing import List, Dict, Optional
|
6 |
|
7 |
+
class GifChatBot:
|
8 |
+
def __init__(self):
|
9 |
+
self.openai_client = None
|
10 |
+
self.giphy_key = None
|
11 |
+
self.chat_history = []
|
12 |
+
|
13 |
+
def setup_keys(self, openai_key: str, giphy_key: str) -> str:
|
14 |
+
"""Initialize API clients with user's keys"""
|
15 |
+
try:
|
16 |
+
self.openai_client = OpenAI(api_key=openai_key)
|
17 |
+
self.giphy_key = giphy_key
|
18 |
+
# Test the keys
|
19 |
+
self._test_giphy_key()
|
20 |
+
self._test_openai_key()
|
21 |
+
return "β
Setup successful! Let's chat!"
|
22 |
+
except Exception as e:
|
23 |
+
return f"β Error setting up: {str(e)}"
|
24 |
+
|
25 |
+
def _test_giphy_key(self):
|
26 |
+
"""Test if GIPHY key is valid"""
|
27 |
+
response = requests.get(
|
28 |
+
"https://api.giphy.com/v1/gifs/trending",
|
29 |
+
params={"api_key": self.giphy_key, "limit": 1}
|
30 |
+
)
|
31 |
+
if response.status_code != 200:
|
32 |
+
raise Exception("Invalid GIPHY API key")
|
33 |
+
|
34 |
+
def _test_openai_key(self):
|
35 |
+
"""Test if OpenAI key is valid"""
|
36 |
+
try:
|
37 |
+
self.openai_client.chat.completions.create(
|
38 |
+
model="gpt-4o-mini",
|
39 |
+
messages=[{"role": "user", "content": "test"}],
|
40 |
+
max_tokens=5
|
41 |
+
)
|
42 |
+
except Exception:
|
43 |
+
raise Exception("Invalid OpenAI API key")
|
44 |
|
45 |
+
def get_gif(self, search_query: str) -> Optional[str]:
|
46 |
+
"""Search GIPHY for a relevant GIF"""
|
47 |
+
try:
|
48 |
+
response = requests.get(
|
49 |
+
"https://api.giphy.com/v1/gifs/search",
|
50 |
+
params={
|
51 |
+
"api_key": self.giphy_key,
|
52 |
+
"q": search_query,
|
53 |
+
"limit": 10,
|
54 |
+
"rating": "pg-13"
|
55 |
+
}
|
56 |
+
)
|
57 |
+
if response.status_code == 200:
|
58 |
+
data = response.json()
|
59 |
+
if data["data"]:
|
60 |
+
import random
|
61 |
+
gif = random.choice(data["data"])
|
62 |
+
return gif["images"]["original"]["url"]
|
63 |
+
except Exception as e:
|
64 |
+
print(f"GIPHY error: {e}")
|
65 |
+
return None
|
66 |
|
67 |
+
def chat(self, message: str, history: List[List[str]]) -> tuple[str, List[List[str]]]:
|
68 |
+
"""Main chat function with GIF support"""
|
69 |
+
if not self.openai_client or not self.giphy_key:
|
70 |
+
return "Please set up your API keys first!", history
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
try:
|
73 |
+
# Create the system message that encourages proactive GIF usage
|
74 |
+
system_message = """You are a fun, internet-savvy friend who loves expressing yourself through GIFs!
|
75 |
+
You can and should proactively use GIFs when appropriate to make the conversation more engaging.
|
76 |
+
|
77 |
+
To use a GIF, simply include [GIF: description] in your message where you want the GIF to appear.
|
78 |
+
For example: "That's amazing! [GIF: mind blown explosion] I can't believe it!"
|
79 |
+
|
80 |
+
Keep your responses conversational and natural, and use GIFs to enhance emotional expression or reactions.
|
81 |
+
Don't overuse GIFs - use them naturally like a real person would in chat."""
|
82 |
|
83 |
+
# Prepare conversation history
|
84 |
+
messages = [{"role": "system", "content": system_message}]
|
85 |
+
for h in history:
|
86 |
+
messages.extend([
|
87 |
+
{"role": "user", "content": h[0]},
|
88 |
+
{"role": "assistant", "content": h[1]}
|
89 |
+
])
|
90 |
+
messages.append({"role": "user", "content": message})
|
91 |
|
92 |
+
# Get AI response
|
93 |
+
response = self.openai_client.chat.completions.create(
|
94 |
+
model="gpt-4o-mini",
|
95 |
+
messages=messages,
|
96 |
+
temperature=0.9,
|
97 |
+
max_tokens=150
|
98 |
+
)
|
99 |
|
100 |
+
# Process response and insert GIFs
|
101 |
+
ai_message = response.choices[0].message.content
|
102 |
+
final_response = ""
|
103 |
+
|
104 |
+
# Split by GIF markers and process
|
105 |
+
parts = ai_message.split("[GIF:")
|
106 |
+
final_response += parts[0]
|
107 |
+
|
108 |
+
for part in parts[1:]:
|
109 |
+
gif_desc_end = part.find("]")
|
110 |
+
if gif_desc_end != -1:
|
111 |
+
gif_desc = part[:gif_desc_end].strip()
|
112 |
+
gif_url = self.get_gif(gif_desc)
|
113 |
+
if gif_url:
|
114 |
+
final_response += f"\n\n"
|
115 |
+
final_response += part[gif_desc_end + 1:]
|
116 |
|
117 |
+
history.append([message, final_response])
|
118 |
+
return final_response, history
|
119 |
|
120 |
+
except Exception as e:
|
121 |
+
return f"Oops! Something went wrong: {str(e)}", history
|
122 |
|
123 |
+
# Create Gradio interface
|
124 |
+
def create_interface():
|
125 |
+
bot = GifChatBot()
|
126 |
+
|
127 |
+
with gr.Blocks() as interface:
|
128 |
+
gr.Markdown("""
|
129 |
+
# Fun AI Friend with GIFs! π
|
130 |
+
First, enter your API keys below to get started. Then chat away!
|
131 |
+
""")
|
132 |
+
|
133 |
+
with gr.Row():
|
134 |
+
openai_key = gr.Textbox(
|
135 |
+
label="OpenAI API Key",
|
136 |
+
placeholder="sk-...",
|
137 |
+
type="password"
|
138 |
+
)
|
139 |
+
giphy_key = gr.Textbox(
|
140 |
+
label="GIPHY API Key",
|
141 |
+
placeholder="Enter your GIPHY API key",
|
142 |
+
type="password"
|
143 |
+
)
|
144 |
+
|
145 |
+
setup_button = gr.Button("Set up Keys")
|
146 |
+
setup_status = gr.Textbox(label="Setup Status")
|
147 |
+
|
148 |
+
chatbot = gr.Chatbot(
|
149 |
+
label="Chat",
|
150 |
+
bubble_full_width=False,
|
151 |
+
show_label=False,
|
152 |
+
height=400
|
153 |
+
)
|
154 |
+
msg = gr.Textbox(
|
155 |
+
label="Type your message",
|
156 |
+
placeholder="Say something...",
|
157 |
+
show_label=False
|
158 |
+
)
|
159 |
+
clear = gr.Button("Clear Chat")
|
160 |
|
161 |
+
# Set up event handlers
|
162 |
+
setup_button.click(
|
163 |
+
bot.setup_keys,
|
164 |
+
inputs=[openai_key, giphy_key],
|
165 |
+
outputs=setup_status
|
166 |
+
)
|
167 |
+
|
168 |
+
msg.submit(
|
169 |
+
bot.chat,
|
170 |
+
inputs=[msg, chatbot],
|
171 |
+
outputs=[msg, chatbot]
|
172 |
+
)
|
173 |
+
|
174 |
+
clear.click(lambda: None, None, chatbot)
|
175 |
+
|
176 |
+
gr.Markdown("""
|
177 |
+
### Notes:
|
178 |
+
- The AI will naturally use GIFs when appropriate in conversation
|
179 |
+
- Your API keys are never stored and are only used for this session
|
180 |
+
- Have fun chatting! π
|
181 |
+
""")
|
182 |
+
|
183 |
+
return interface
|
184 |
|
185 |
+
# Launch the app
|
186 |
if __name__ == "__main__":
|
187 |
+
demo = create_interface()
|
188 |
+
demo.launch()
|