File size: 1,183 Bytes
9a0ab7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import gradio as gr
import json

def get_random_quote(lang='en'):
    """
    Fetches a random quote from the Forismatic API.

    Args:
        lang (str): Language code for the quote. Default is 'en'.

    Returns:
        str: Formatted quote string with author.
    """
    url = "http://api.forismatic.com/api/1.0/"
    params = {
        'method': 'getQuote',
        'lang': lang,
        'format': 'json'
    }

    response = requests.get(url, params=params)

    try:
        # Try to decode raw JSON safely
        cleaned_text = response.text.replace('\\', '\\\\')
        data = json.loads(cleaned_text)
    except json.JSONDecodeError:
        return "Failed to parse quote. Please try again."

    quote = data.get('quoteText', 'No quote available').strip()
    author = data.get('quoteAuthor', 'Unknown').strip()

    return f'"{quote}"\n— {author}'

# Create Gradio interface
app = gr.Interface(
    fn=get_random_quote,
    inputs=[],
    outputs=gr.Textbox(label="Random Quote"),
    title="Random Quote Generator",
    description="Click the button to get an inspiring random quote."
)

if __name__ == "__main__":
    app.launch(mcp_server=True)