Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from playwright.sync_api import sync_playwright
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
|
| 6 |
+
@app.get("/chat")
|
| 7 |
+
async def chat(query: str):
|
| 8 |
+
with sync_playwright() as p:
|
| 9 |
+
# Launch the Chromium browser
|
| 10 |
+
browser = p.chromium.launch(headless=True) # Set to False to see the browser
|
| 11 |
+
page = browser.new_page()
|
| 12 |
+
|
| 13 |
+
# Navigate to ChatGPT page
|
| 14 |
+
page.goto("https://chatgpt.com")
|
| 15 |
+
|
| 16 |
+
# Wait for the chat input area to load
|
| 17 |
+
page.wait_for_selector('textarea[placeholder="Type a message…"]')
|
| 18 |
+
|
| 19 |
+
# Type a message into the chat input
|
| 20 |
+
page.fill('textarea[placeholder="Type a message…"]', query)
|
| 21 |
+
|
| 22 |
+
# Press Enter to send the message
|
| 23 |
+
page.press('textarea[placeholder="Type a message…"]', 'Enter')
|
| 24 |
+
|
| 25 |
+
# Wait for the response (adjust the wait time as necessary)
|
| 26 |
+
page.wait_for_selector('div[class*="message-response-class"]') # Adjust the class name
|
| 27 |
+
|
| 28 |
+
# Extract the response text
|
| 29 |
+
response = page.query_selector('div[class*="message-response-class"]') # Adjust the class name
|
| 30 |
+
message = response.inner_text() if response else "No response found."
|
| 31 |
+
|
| 32 |
+
# Close the browser
|
| 33 |
+
browser.close()
|
| 34 |
+
|
| 35 |
+
return {"response": message}
|