File size: 1,359 Bytes
f6d601d
 
0569a5d
 
f6d601d
0569a5d
 
 
 
 
 
8f6b608
 
0569a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab143e
 
0569a5d
 
8f6b608
 
0569a5d
a270fa4
f6d601d
1
2
3
4
5
6
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# x.com/xyizko

import gradio as gr
import asyncio
from jokeapi import Jokes

def jokerz():
    """Main function containing all sub-functions for fetching and displaying jokes."""

    async def get_joke():
        """Fetch a random joke asynchronously from JokeAPI."""
        j = await Jokes()  
        joke = await j.get_joke()  

        if isinstance(joke, list):  
            joke = joke[0]  

        return (
            joke["joke"]
            if joke["type"] == "single"
            else f"{joke['setup']} - {joke['delivery']}"
        )

    def fetch_joke():
        """Handles sync & async cases without crashing."""
        try:
            return asyncio.run(get_joke())  
        except RuntimeError:
            loop = asyncio.get_event_loop()
            return loop.run_until_complete(get_joke())

    def get_joke_response(message, history):
        """Handles incoming chat messages and returns a joke."""
        return fetch_joke()

    def run_interface():
        """Creates and returns the Gradio ChatInterface."""
        return gr.ChatInterface(
            get_joke_response,
            type="messages",
            title=" xyizko - Random Joke Generator 🤣",
            description="Get random jokes from JokeAPI!",
        )

    jokeapp = run_interface()
    jokeapp.launch()  

if __name__ == "__main__":
    jokerz()