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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
# β
Securely get API key from environment variables
|
| 7 |
+
gemini_api_key = os.environ.get("GEMINI_API_KEY")
|
| 8 |
+
if not gemini_api_key:
|
| 9 |
+
raise ValueError("""
|
| 10 |
+
β οΈ ERROR: Gemini API Key is missing!
|
| 11 |
+
Set your API key in environment variables using:
|
| 12 |
+
export GEMINI_API_KEY='your-api-key-here'
|
| 13 |
+
""")
|
| 14 |
+
|
| 15 |
+
# β
Configure Gemini API
|
| 16 |
+
genai.configure(api_key=gemini_api_key)
|
| 17 |
+
|
| 18 |
+
def get_joke(language):
|
| 19 |
+
"""Get a joke in the selected language (English or Hinglish)"""
|
| 20 |
+
try:
|
| 21 |
+
model = genai.GenerativeModel("models/gemini-2.0-flash-lite")
|
| 22 |
+
|
| 23 |
+
# β
Random funny words for more variety
|
| 24 |
+
random_word = random.choice([
|
| 25 |
+
"banana", "robot", "pirate", "spaceship", "wizard", "penguin",
|
| 26 |
+
"vampire", "dinosaur", "detective", "ninja", "zombie", "alien",
|
| 27 |
+
"astronaut", "time traveler", "cowboy", "unicorn", "octopus",
|
| 28 |
+
"squirrel", "chicken", "ghost", "superhero", "scientist",
|
| 29 |
+
"teacher", "doctor", "chef", "dog", "cat", "hamster", "elephant",
|
| 30 |
+
"monkey", "koala", "turtle", "cactus", "caveman", "jellyfish",
|
| 31 |
+
"kangaroo", "mermaid", "gnome", "fairy", "dragon", "owl",
|
| 32 |
+
"skeleton", "t-rex", "werewolf", "bigfoot", "genie", "goldfish",
|
| 33 |
+
"duck", "giraffe", "parrot", "sloth", "teddy bear", "panda",
|
| 34 |
+
"penguin", "robot", "unicorn", "vampire", "zombie", "alien"
|
| 35 |
+
])
|
| 36 |
+
|
| 37 |
+
# β
Language-based prompt selection
|
| 38 |
+
if language == "English":
|
| 39 |
+
prompt = f"Tell me a funny, unique joke about {random_word}. Keep it in simple, clear English."
|
| 40 |
+
else: # Hinglish (Mix of Hindi & English)
|
| 41 |
+
prompt = f"Ek funny joke sunao {random_word} ke baare mein, jo thoda Hindi aur thoda English mila ho. Thoda desi touch ho!"
|
| 42 |
+
|
| 43 |
+
response = model.generate_content(
|
| 44 |
+
prompt,
|
| 45 |
+
generation_config={
|
| 46 |
+
"temperature": 1.2, # More creativity
|
| 47 |
+
"top_p": 0.9, # Diverse responses
|
| 48 |
+
"max_output_tokens": 100 # Limit response length
|
| 49 |
+
}
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
return response.text if response and hasattr(response, 'text') else "β οΈ No joke found."
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"β οΈ Error generating joke: {str(e)}"
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def rate_joke(feedback):
|
| 59 |
+
"""Handle user feedback"""
|
| 60 |
+
return f"Thanks for your feedback! You rated: {feedback}"
|
| 61 |
+
|
| 62 |
+
# β
Improved Gradio UI with a dark striped background
|
| 63 |
+
with gr.Blocks(css="""
|
| 64 |
+
body {
|
| 65 |
+
background-color: #000000; /* Changed to Black */
|
| 66 |
+
}
|
| 67 |
+
h1, h3 {
|
| 68 |
+
font-family: Arial, sans-serif;
|
| 69 |
+
text-align: center;
|
| 70 |
+
color: white;
|
| 71 |
+
}
|
| 72 |
+
#joke-box {
|
| 73 |
+
background-color: #D3D3D3;
|
| 74 |
+
color: white;
|
| 75 |
+
border-radius: 10px;
|
| 76 |
+
font-size: 32px; /* Increased size */
|
| 77 |
+
font-weight: bold; /* Bold text */
|
| 78 |
+
font-family: 'Comic Sans MS', cursive; /* Fun font */
|
| 79 |
+
padding: 20px;
|
| 80 |
+
}
|
| 81 |
+
#joke-button {
|
| 82 |
+
background: #8A2BE2; /* Purple */
|
| 83 |
+
color: white;
|
| 84 |
+
font-weight: bold;
|
| 85 |
+
font-size: 1.2em;
|
| 86 |
+
border-radius: 10px;
|
| 87 |
+
padding: 10px;
|
| 88 |
+
}
|
| 89 |
+
#feedback-buttons {
|
| 90 |
+
display: flex;
|
| 91 |
+
justify-content: center;
|
| 92 |
+
gap: 10px;
|
| 93 |
+
}
|
| 94 |
+
.funny-button {
|
| 95 |
+
background-color: #00C853; /* Bright Green */
|
| 96 |
+
color: white;
|
| 97 |
+
font-weight: bold;
|
| 98 |
+
font-size: 1.2em;
|
| 99 |
+
border-radius: 10px;
|
| 100 |
+
padding: 10px;
|
| 101 |
+
}
|
| 102 |
+
.not-funny-button {
|
| 103 |
+
background-color: #D32F2F; /* Deep Red */
|
| 104 |
+
color: white;
|
| 105 |
+
font-weight: bold;
|
| 106 |
+
font-size: 1.2em;
|
| 107 |
+
border-radius: 10px;
|
| 108 |
+
padding: 10px;
|
| 109 |
+
}
|
| 110 |
+
""") as app:
|
| 111 |
+
|
| 112 |
+
gr.Markdown("""
|
| 113 |
+
<div style="text-align: center; font-family: Arial, sans-serif;">
|
| 114 |
+
<h1 style="font-size: 2.8em; font-weight: bold;">
|
| 115 |
+
π€£ AI'S GOT LATENT π€£
|
| 116 |
+
</h1>
|
| 117 |
+
<h3 style="font-size: 2em;">
|
| 118 |
+
Don't worry only family friendly jokes!π
|
| 119 |
+
</h3>
|
| 120 |
+
</div>
|
| 121 |
+
""")
|
| 122 |
+
|
| 123 |
+
with gr.Column(elem_id="main-container", scale=1, min_width=1000):
|
| 124 |
+
|
| 125 |
+
# β
Language selection dropdown
|
| 126 |
+
language_dropdown = gr.Dropdown(
|
| 127 |
+
choices=["English", "Hinglish"],
|
| 128 |
+
value="English",
|
| 129 |
+
label="Select Joke Language"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# β
Output box
|
| 133 |
+
joke_output = gr.Textbox(
|
| 134 |
+
placeholder="Your joke will appear here...",
|
| 135 |
+
interactive=False,
|
| 136 |
+
lines=5,
|
| 137 |
+
show_label=False,
|
| 138 |
+
elem_id="joke-box"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# β
Joke button (Purple)
|
| 142 |
+
joke_button = gr.Button(
|
| 143 |
+
"π² Generate a Joke", variant="primary",
|
| 144 |
+
elem_id="joke-button"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
gr.Markdown("""
|
| 148 |
+
<div style="text-align: center; font-size: 2em; margin-top: 20px; color: white;">
|
| 149 |
+
<strong>Did you like this joke?</strong>
|
| 150 |
+
</div>
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
with gr.Row(elem_id="feedback-buttons", equal_height=True):
|
| 154 |
+
thumbs_up = gr.Button("π Funny!", elem_classes=["funny-button"]) # Green
|
| 155 |
+
thumbs_down = gr.Button("π Not Funny", elem_classes=["not-funny-button"]) # Red
|
| 156 |
+
|
| 157 |
+
feedback_output = gr.Textbox(
|
| 158 |
+
label="Feedback",
|
| 159 |
+
interactive=False,
|
| 160 |
+
visible=True,
|
| 161 |
+
elem_id="feedback-box"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# β
Button click now includes language selection
|
| 165 |
+
joke_button.click(get_joke, inputs=language_dropdown, outputs=joke_output)
|
| 166 |
+
thumbs_up.click(lambda: rate_joke("π Funny!"), outputs=feedback_output)
|
| 167 |
+
thumbs_down.click(lambda: rate_joke("π Not Funny"), outputs=feedback_output)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# β
Secure launch configuration
|
| 172 |
+
if __name__ == "__main__":
|
| 173 |
+
app.launch(
|
| 174 |
+
share=True,
|
| 175 |
+
debug=False
|
| 176 |
+
)
|