import asyncio
from js import document, console
from pyodide.http import pyfetch
import json
async def send_to_chatgpt(text):
try:
url = 'https://api.openai.com/v1/chat/completions'
headers = {
'Authorization': 'Bearer your-api-key-here',
'Content-Type': 'application/json'
}
payload = {
'model': 'gpt-4-mini',
'messages': [{'role': 'user', 'content': text}],
'temperature': 1,
'max_tokens': 16383
}
response = await pyfetch(
url,
method='POST',
headers=headers,
body=json.dumps(payload)
)
result = await response.json()
return result['choices'][0]['message']['content']
except Exception as e:
console.error(f"Error: {str(e)}")
return f"Error: {str(e)}"
async def handle_text(text):
try:
response = await send_to_chatgpt(text)
document.getElementById('outputText').value = response
except Exception as e:
console.error(f"Error: {str(e)}")
document.getElementById('outputText').value = f"Error: {str(e)}"
def js_handle_text(text):
asyncio.ensure_future(handle_text(text))