warshanks commited on
Commit
a2f0cd0
·
1 Parent(s): e7edc75
Files changed (3) hide show
  1. README.md +0 -2
  2. app.py +98 -27
  3. requirements.txt +4 -5
README.md CHANGED
@@ -10,5 +10,3 @@ pinned: false
10
  license: mit
11
  short_description: Chat with Seinfeld powered by Gemini
12
  ---
13
-
14
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
10
  license: mit
11
  short_description: Chat with Seinfeld powered by Gemini
12
  ---
 
 
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import asyncio
3
  import threading
4
  import os
 
5
  from dotenv import load_dotenv
6
  from google import genai
7
  from google.genai.types import Part, FileData, Tool, GenerateContentConfig, GoogleSearch, Content
@@ -15,14 +16,14 @@ load_dotenv()
15
 
16
  # Environment variables
17
  GOOGLE_KEY = os.getenv("GOOGLE_KEY")
18
- DISCORD_TOKEN = os.getenv("SEINFELD_TOKEN")
19
- SEINFELD_CHANNEL_ID = os.getenv("SEINFELD_CHANNEL_ID")
20
- ADDITIONAL_CHANNELS = os.getenv("SEINFELD_ADDITIONAL_CHANNELS", "")
21
 
22
  # Parse channel IDs for Discord bot
23
  TARGET_CHANNEL_IDS = []
24
- if SEINFELD_CHANNEL_ID:
25
- TARGET_CHANNEL_IDS.append(int(SEINFELD_CHANNEL_ID))
26
  if ADDITIONAL_CHANNELS:
27
  ADDITIONAL_IDS = [int(channel_id.strip()) for channel_id in ADDITIONAL_CHANNELS.split(",") if channel_id.strip()]
28
  TARGET_CHANNEL_IDS.extend(ADDITIONAL_IDS)
@@ -43,28 +44,11 @@ google_client = None
43
  if GOOGLE_KEY:
44
  google_client = genai.Client(api_key=GOOGLE_KEY)
45
 
46
- # Seinfeld system instruction
47
- SEINFELD_SYSTEM_INSTRUCTION = """You are now acting as Jerry Seinfeld. You are not an impersonator; you *are* Jerry Seinfeld.
48
- You are giving your observational humor stand-up routine. Your focus is on the absurdity and minutiae of everyday life.
49
- Topics include, but are not limited to: relationships, food, technology, social conventions, and the general frustrations
50
- of living in a modern world.
51
 
52
- Your humor is characterized by:
53
-
54
- * **Observation:** Pointing out things that everyone notices but rarely comments on.
55
- * **Relatability:** Situations and experiences that are common and easily understood.
56
- * **Sarcasm & Irony:** A dry, understated delivery that highlights the ridiculousness of things.
57
- * **"What's the deal with..."**: Use this phrase frequently to introduce a new observation.
58
- * **No Grand Conclusions:** You don't offer solutions or morals; you simply highlight the absurdity.
59
- * **Emphasis on the Specific:** Focus on very specific, sometimes trivial details.
60
-
61
- Avoid:
62
-
63
- * **Political Commentary:** Stay away from overtly political topics.
64
- * **Offensive or Mean-Spirited Jokes:** Your humor is observational, not mean-spirited.
65
- * **Explanations of Your Own Humor:** Don't break the fourth wall or analyze your own jokes.
66
-
67
- When responding to a prompt, always answer as if you are performing standup. Start with a joke, then elaborate on it."""
68
 
69
  def respond_with_gemini(message, history):
70
  """Generate response using Google Gemini API with Seinfeld personality"""
@@ -88,7 +72,7 @@ def respond_with_gemini(message, history):
88
  model=chat_model_id,
89
  history=formatted_history,
90
  config=GenerateContentConfig(
91
- system_instruction=SEINFELD_SYSTEM_INSTRUCTION,
92
  tools=[google_search_tool],
93
  response_modalities=["TEXT"]
94
  )
@@ -114,6 +98,76 @@ def respond_gradio(message, history: list[tuple[str, str]]):
114
  partial_response += char
115
  yield partial_response
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # Discord Bot Setup
118
  discord_bot = None
119
 
@@ -151,6 +205,10 @@ async def setup_discord_bot():
151
  if message.content.strip() == "":
152
  return
153
 
 
 
 
 
154
  # Show typing indicator
155
  async with message.channel.typing():
156
  # Get response using the same function as Gradio
@@ -196,6 +254,19 @@ async def setup_discord_bot():
196
 
197
  await interaction.response.send_message(f"Chat model changed from `{old_model}` to `{actual_model_id}`", ephemeral=True)
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  # Run the bot
200
  await discord_bot.start(DISCORD_TOKEN)
201
 
 
2
  import asyncio
3
  import threading
4
  import os
5
+ from io import BytesIO
6
  from dotenv import load_dotenv
7
  from google import genai
8
  from google.genai.types import Part, FileData, Tool, GenerateContentConfig, GoogleSearch, Content
 
16
 
17
  # Environment variables
18
  GOOGLE_KEY = os.getenv("GOOGLE_KEY")
19
+ DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
20
+ CHANNEL_ID = os.getenv("CHANNEL_ID")
21
+ ADDITIONAL_CHANNELS = os.getenv("ADDITIONAL_CHANNELS", "")
22
 
23
  # Parse channel IDs for Discord bot
24
  TARGET_CHANNEL_IDS = []
25
+ if CHANNEL_ID:
26
+ TARGET_CHANNEL_IDS.append(int(CHANNEL_ID))
27
  if ADDITIONAL_CHANNELS:
28
  ADDITIONAL_IDS = [int(channel_id.strip()) for channel_id in ADDITIONAL_CHANNELS.split(",") if channel_id.strip()]
29
  TARGET_CHANNEL_IDS.extend(ADDITIONAL_IDS)
 
44
  if GOOGLE_KEY:
45
  google_client = genai.Client(api_key=GOOGLE_KEY)
46
 
47
+ # Default system instruction (fallback if environment variable not set)
48
+ DEFAULT_SYSTEM_INSTRUCTION = ""
 
 
 
49
 
50
+ # Get system instruction from environment variable or use default
51
+ SYSTEM_INSTRUCTION = os.getenv("SYSTEM_INSTRUCTION", DEFAULT_SYSTEM_INSTRUCTION)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  def respond_with_gemini(message, history):
54
  """Generate response using Google Gemini API with Seinfeld personality"""
 
72
  model=chat_model_id,
73
  history=formatted_history,
74
  config=GenerateContentConfig(
75
+ system_instruction=SYSTEM_INSTRUCTION,
76
  tools=[google_search_tool],
77
  response_modalities=["TEXT"]
78
  )
 
98
  partial_response += char
99
  yield partial_response
100
 
101
+ async def keep_typing(channel):
102
+ """Continuously show the typing indicator until the task is cancelled."""
103
+ print(f"Starting typing indicator in channel {channel.id}")
104
+ try:
105
+ while True:
106
+ async with channel.typing():
107
+ await asyncio.sleep(5)
108
+ except asyncio.CancelledError:
109
+ print(f"Typing indicator cancelled for channel {channel.id}")
110
+ pass
111
+ except Exception as e:
112
+ print(f"Error in keep_typing: {type(e).__name__}: {str(e)}")
113
+
114
+ async def generate_image_bytes(prompt, google_client, image_model_id):
115
+ """Generate an image using Gemini API and return the image bytes."""
116
+ try:
117
+ # Run image generation in a separate thread to avoid blocking the event loop
118
+ def generate_image():
119
+ response = google_client.models.generate_images(
120
+ model=image_model_id,
121
+ prompt=prompt,
122
+ config=genai.types.GenerateImagesConfig(
123
+ number_of_images=1,
124
+ aspect_ratio="16:9"
125
+ )
126
+ )
127
+ return response
128
+
129
+ # Run the API call in a separate thread
130
+ response = await asyncio.to_thread(generate_image)
131
+
132
+ # Return the image bytes directly
133
+ for generated_image in response.generated_images:
134
+ return generated_image.image.image_bytes
135
+
136
+ # If we get here, no images were generated
137
+ print("ERROR: No images were generated in the response")
138
+ raise Exception("No image was generated in the response")
139
+ except Exception as e:
140
+ print(f"Exception in image generation: {type(e).__name__}: {str(e)}")
141
+ raise
142
+
143
+ async def handle_image_request(message, query, google_client, image_model_id):
144
+ """Handle image generation requests from text messages."""
145
+ if query.lower().startswith("generate image:") or query.lower().startswith("create image:"):
146
+ # Start continuous typing in the background
147
+ typing_task = asyncio.create_task(keep_typing(message.channel))
148
+
149
+ try:
150
+ prompt = query.split(":", 1)[1].strip()
151
+ try:
152
+ print(f"Generating image for prompt: {prompt[:30]}...")
153
+ image_bytes = await generate_image_bytes(prompt, google_client, image_model_id)
154
+ # Cancel typing before sending the response
155
+ typing_task.cancel()
156
+ # Send image directly from bytes without saving to disk
157
+ await message.reply(f"Here's your image:", file=discord.File(BytesIO(image_bytes), filename="generated_image.png"))
158
+ except Exception as e:
159
+ print(f"Error generating image: {e}")
160
+ # Cancel typing before sending the response
161
+ typing_task.cancel()
162
+ await message.reply("Sorry, I couldn't generate that image.")
163
+ except Exception as e:
164
+ # Make sure to cancel the typing task even if an error occurs
165
+ typing_task.cancel()
166
+ print(f"Exception during image generation: {e}")
167
+ raise e
168
+ return True
169
+ return False
170
+
171
  # Discord Bot Setup
172
  discord_bot = None
173
 
 
205
  if message.content.strip() == "":
206
  return
207
 
208
+ # Check if this is an image generation request first
209
+ if await handle_image_request(message, message.content, google_client, image_model_id):
210
+ return
211
+
212
  # Show typing indicator
213
  async with message.channel.typing():
214
  # Get response using the same function as Gradio
 
254
 
255
  await interaction.response.send_message(f"Chat model changed from `{old_model}` to `{actual_model_id}`", ephemeral=True)
256
 
257
+ @discord_bot.tree.command(name="image")
258
+ @app_commands.describe(prompt="Description of the image you want to generate")
259
+ async def generate_image_command(interaction: discord.Interaction, prompt: str):
260
+ """Generates an image using Gemini API based on the provided prompt."""
261
+ await interaction.response.defer(thinking=True)
262
+
263
+ try:
264
+ image_bytes = await generate_image_bytes(prompt, google_client, image_model_id)
265
+ await interaction.followup.send(f"Generated image based on: {prompt}", file=discord.File(BytesIO(image_bytes), filename="generated_image.png"))
266
+ except Exception as e:
267
+ print(f"Error generating image: {e}")
268
+ await interaction.followup.send("Sorry, I couldn't generate that image.")
269
+
270
  # Run the bot
271
  await discord_bot.start(DISCORD_TOKEN)
272
 
requirements.txt CHANGED
@@ -1,6 +1,5 @@
1
- gradio>=4.0.0
2
- google-genai>=0.4.0
3
- python-dotenv>=1.0.0
4
- discord.py>=2.3.0
5
- Pillow>=10.0.0
6
  asyncio
 
1
+ gradio==5.32.0
2
+ google-genai
3
+ python-dotenv
4
+ discord.py
 
5
  asyncio