Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
|
5 |
+
def get_random_quote(lang='en'):
|
6 |
+
"""
|
7 |
+
Fetches a random quote from the Forismatic API.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
lang (str): Language code for the quote. Default is 'en'.
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
str: Formatted quote string with author.
|
14 |
+
"""
|
15 |
+
url = "http://api.forismatic.com/api/1.0/"
|
16 |
+
params = {
|
17 |
+
'method': 'getQuote',
|
18 |
+
'lang': lang,
|
19 |
+
'format': 'json'
|
20 |
+
}
|
21 |
+
|
22 |
+
response = requests.get(url, params=params)
|
23 |
+
|
24 |
+
try:
|
25 |
+
# Try to decode raw JSON safely
|
26 |
+
cleaned_text = response.text.replace('\\', '\\\\')
|
27 |
+
data = json.loads(cleaned_text)
|
28 |
+
except json.JSONDecodeError:
|
29 |
+
return "Failed to parse quote. Please try again."
|
30 |
+
|
31 |
+
quote = data.get('quoteText', 'No quote available').strip()
|
32 |
+
author = data.get('quoteAuthor', 'Unknown').strip()
|
33 |
+
|
34 |
+
return f'"{quote}"\n— {author}'
|
35 |
+
|
36 |
+
# Create Gradio interface
|
37 |
+
app = gr.Interface(
|
38 |
+
fn=get_random_quote,
|
39 |
+
inputs=[],
|
40 |
+
outputs=gr.Textbox(label="Random Quote"),
|
41 |
+
title="Random Quote Generator",
|
42 |
+
description="Click the button to get an inspiring random quote."
|
43 |
+
)
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
app.launch(mcp_server=True)
|