NihalGazi commited on
Commit
10a2f54
·
verified ·
1 Parent(s): cd6d8b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -2,7 +2,7 @@ import re
2
  import json
3
  import requests
4
  from fastapi import FastAPI
5
- from fastapi.responses import HTMLResponse
6
 
7
  app = FastAPI()
8
 
@@ -32,11 +32,15 @@ Format:
32
  }
33
  """
34
 
35
- def get_wiki_json(topic: str):
36
- """Fetch Wikipedia-style JSON article from Pollinations.ai"""
37
  prompt = PROMPT_TEMPLATE.format(topic=topic)
38
  r = requests.get("https://text.pollinations.ai/prompt/" + prompt)
39
- text = r.text.strip()
 
 
 
 
40
 
41
  # Extract substring between first { and last }
42
  start = text.find("{")
@@ -53,7 +57,6 @@ def get_wiki_json(topic: str):
53
 
54
  return data
55
 
56
-
57
  def render_page(data: dict) -> str:
58
  """Convert parsed JSON into a simple HTML page"""
59
  html = f"<html><head><title>{data['title']}</title></head><body>"
@@ -70,6 +73,9 @@ def render_page(data: dict) -> str:
70
  html += "</body></html>"
71
  return html
72
 
 
 
 
73
  @app.get("/wikipedia/{topic}", response_class=HTMLResponse)
74
  def wikipedia(topic: str):
75
  """
@@ -81,4 +87,17 @@ def wikipedia(topic: str):
81
  html = render_page(data)
82
  return HTMLResponse(content=html, status_code=200)
83
  except Exception as e:
84
- return HTMLResponse(content=f"<h1>Error</h1><p>{str(e)}</p>", status_code=500)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import json
3
  import requests
4
  from fastapi import FastAPI
5
+ from fastapi.responses import HTMLResponse, PlainTextResponse
6
 
7
  app = FastAPI()
8
 
 
32
  }
33
  """
34
 
35
+ def fetch_pollinations(topic: str) -> str:
36
+ """Call Pollinations API and return raw text"""
37
  prompt = PROMPT_TEMPLATE.format(topic=topic)
38
  r = requests.get("https://text.pollinations.ai/prompt/" + prompt)
39
+ return r.text.strip()
40
+
41
+ def get_wiki_json(topic: str):
42
+ """Fetch Wikipedia-style JSON article from Pollinations.ai"""
43
+ text = fetch_pollinations(topic)
44
 
45
  # Extract substring between first { and last }
46
  start = text.find("{")
 
57
 
58
  return data
59
 
 
60
  def render_page(data: dict) -> str:
61
  """Convert parsed JSON into a simple HTML page"""
62
  html = f"<html><head><title>{data['title']}</title></head><body>"
 
73
  html += "</body></html>"
74
  return html
75
 
76
+
77
+ # ---- ROUTES ----
78
+
79
  @app.get("/wikipedia/{topic}", response_class=HTMLResponse)
80
  def wikipedia(topic: str):
81
  """
 
87
  html = render_page(data)
88
  return HTMLResponse(content=html, status_code=200)
89
  except Exception as e:
90
+ return HTMLResponse(content=f"<h1>Error</h1><pre>{str(e)}</pre>", status_code=500)
91
+
92
+
93
+ @app.get("/raw/{topic}", response_class=PlainTextResponse)
94
+ def raw(topic: str):
95
+ """
96
+ Debug: show the raw Pollinations output before JSON parsing.
97
+ Example: /raw/Quantum%20Computing
98
+ """
99
+ try:
100
+ text = fetch_pollinations(topic)
101
+ return PlainTextResponse(text, status_code=200)
102
+ except Exception as e:
103
+ return PlainTextResponse(f"Error fetching Pollinations: {str(e)}", status_code=500)