Forked from romanejaquez/gist:d8dd3b36d39efce7f1c53a6db4969844
Last active
June 26, 2024 22:49
-
-
Save MartialTerran/ac17627796acbc992a1ff640e60c0b2e to your computer and use it in GitHub Desktop.
gemini_pictionary_fullcode.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pygame | |
| import requests | |
| import json | |
| import base64 | |
| # Initialize Pygame | |
| pygame.init() | |
| # Screen dimensions | |
| width, height = 640, 480 | |
| screen = pygame.display.set_mode((width, height)) | |
| pygame.display.set_caption("Pictionary with Gemini") | |
| # Colors | |
| white = (255, 255, 255) | |
| black = (0, 0, 0) | |
| purple = (128, 0, 128) | |
| # Drawing variables | |
| drawing = False | |
| last_pos = (0, 0) | |
| brush_size = 5 | |
| # Button variables | |
| button_width = 200 | |
| button_height = 50 | |
| button_x = (width - button_width) // 2 | |
| validate_button_y = height - button_height - 20 | |
| clear_button_y = validate_button_y - button_height - 20 | |
| # Gemini API credentials | |
| api_key = "YOUR_API_KEY_HERE" | |
| model = "gemini-1.5-pro" | |
| api_url = f"https://api.google.com/generative/v1beta2/models/{model}:generateContent?key={api_key}" | |
| # Function to draw the drawing surface | |
| def draw_surface(): | |
| pygame.draw.rect(screen, white, (0, 0, width, height - 100)) | |
| # Function to draw a button | |
| def draw_button(text, x, y, width, height, color): | |
| font = pygame.font.Font(None, 24) | |
| text_surface = font.render(text, True, white) | |
| text_rect = text_surface.get_rect(center=(x + width // 2, y + height // 2)) | |
| pygame.draw.rect(screen, color, (x, y, width, height)) | |
| screen.blit(text_surface, text_rect) | |
| # Function to send image to Gemini and get guess | |
| def get_gemini_guess(image_data): | |
| headers = {"Content-Type": "application/json"} | |
| data = { | |
| "prompt": { | |
| "text": """ | |
| You are a pictionary player. Given the provided image, identify its contents | |
| and reply in the form of a JSON payload response containing the following properties: | |
| { | |
| "guessedImage": the name of the image drawn | |
| } | |
| """ | |
| }, | |
| "image": {"data": image_data.decode("utf-8")}, | |
| } | |
| response = requests.post(api_url, headers=headers, json=data) | |
| response.raise_for_status() | |
| response_json = response.json() | |
| return response_json["candidates"][0]["output"]["text"] | |
| # Function to display the Gemini guess | |
| def display_guess(guess): | |
| font = pygame.font.Font(None, 36) | |
| text_surface = font.render("Gemini guesses: " + guess, True, purple) | |
| text_rect = text_surface.get_rect(center=(width // 2, height // 2)) | |
| screen.blit(text_surface, text_rect) | |
| # Main game loop | |
| running = True | |
| guess = "" | |
| while running: | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| running = False | |
| elif event.type == pygame.MOUSEBUTTONDOWN: | |
| if event.button == 1: | |
| drawing = True | |
| last_pos = event.pos | |
| elif event.button == 3: | |
| # Right click to clear canvas | |
| draw_surface() | |
| elif event.type == pygame.MOUSEBUTTONUP: | |
| if event.button == 1: | |
| drawing = False | |
| elif event.type == pygame.MOUSEMOTION: | |
| if drawing: | |
| pygame.draw.line(screen, black, last_pos, event.pos, brush_size) | |
| last_pos = event.pos | |
| elif event.type == pygame.KEYDOWN: | |
| if event.key == pygame.K_RETURN: | |
| # Convert the drawing surface to a base64 encoded string | |
| pygame.image.save(screen, "drawing.png") | |
| with open("drawing.png", "rb") as image_file: | |
| image_data = base64.b64encode(image_file.read()) | |
| try: | |
| guess = get_gemini_guess(image_data) | |
| guess = json.loads(guess)["guessedImage"] | |
| except Exception as e: | |
| guess = f"Error: {e}" | |
| # Draw buttons | |
| draw_button("Validate Picture", button_x, validate_button_y, button_width, button_height, purple) | |
| draw_button("Clear", button_x, clear_button_y, button_width, button_height, purple) | |
| # Display Gemini guess | |
| if guess: | |
| display_guess(guess) | |
| # Update display | |
| pygame.display.flip() | |
| pygame.quit() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MartialTerran/Pictionary.py
Forked from romanejaquez/gist:d8dd3b36d39efce7f1c53a6db4969844