SHOW:
|
|
- or go back to the newest paste.
| 1 | # run the below command in your terminal first | |
| 2 | # pip install flask requests | |
| 3 | ||
| 4 | ||
| 5 | from flask import Flask, request, render_template_string | |
| 6 | import requests | |
| 7 | import json | |
| 8 | from datetime import datetime | |
| 9 | ||
| 10 | app = Flask(__name__) | |
| 11 | ||
| 12 | HTML = """ | |
| 13 | <!doctype html> | |
| 14 | <html> | |
| 15 | <head> | |
| 16 | <title>ChatGPT API Interface</title> | |
| 17 | <style> | |
| 18 | body {
| |
| 19 | font-family: Arial, sans-serif; | |
| 20 | background-color: #f4f4f9; | |
| 21 | margin: 40px; | |
| 22 | color: #333; | |
| 23 | } | |
| 24 | form {
| |
| 25 | background: white; | |
| 26 | padding: 20px; | |
| 27 | border-radius: 8px; | |
| 28 | box-shadow: 0 2px 4px rgba(0,0,0,0.1); | |
| 29 | } | |
| 30 | input[type=text], input[type=number], select, textarea {
| |
| 31 | width: 100%; | |
| 32 | padding: 8px; | |
| 33 | margin: 8px 0; | |
| 34 | display: inline-block; | |
| 35 | border: 1px solid #ccc; | |
| 36 | border-radius: 4px; | |
| 37 | box-sizing: border-box; | |
| 38 | } | |
| 39 | input[type=submit], button {
| |
| 40 | width: 100%; | |
| 41 | background-color: #4CAF50; | |
| 42 | color: white; | |
| 43 | padding: 14px 20px; | |
| 44 | margin: 8px 0; | |
| 45 | border: none; | |
| 46 | border-radius: 4px; | |
| 47 | cursor: pointer; | |
| 48 | } | |
| 49 | input[type=submit]:hover, button:hover {
| |
| 50 | background-color: #45a049; | |
| 51 | } | |
| 52 | h2 {
| |
| 53 | color: #333; | |
| 54 | } | |
| 55 | textarea {
| |
| 56 | height: 100px; /* Adjust textarea height */ | |
| 57 | } | |
| 58 | .response {
| |
| 59 | background: white; | |
| 60 | padding: 10px; | |
| 61 | border-radius: 4px; | |
| 62 | margin-top: 20px; | |
| 63 | box-shadow: 0 2px 4px rgba(0,0,0,0.1); | |
| 64 | } | |
| 65 | .response-header {
| |
| 66 | font-weight: bold; | |
| 67 | } | |
| 68 | .response-content {
| |
| 69 | white-space: pre-wrap; /* Maintains whitespace formatting */ | |
| 70 | margin-top: 5px; | |
| 71 | } | |
| 72 | </style> | |
| 73 | </head> | |
| 74 | <body> | |
| 75 | <h2>ChatGPT API Form</h2> | |
| 76 | <form method="post"> | |
| 77 | API Key: <input type="text" name="api_key" value="{{ api_key }}"><br>
| |
| 78 | Model: <select name="model"> | |
| 79 | <option value="gpt-4o" {% if model == 'gpt-4o' %}selected{% endif %}>gpt-4o</option>
| |
| 80 | <option value="gpt-4-turbo" {% if model == 'gpt-4-turbo' %}selected{% endif %}>gpt-4-turbo</option>
| |
| 81 | <option value="gpt-4-turbo-2024-04-09" {% if model == 'gpt-4-turbo-2024-04-09' %}selected{% endif %}>gpt-4-turbo-2024-04-09</option>
| |
| 82 | <option value="gpt-4" {% if model == 'gpt-4' %}selected{% endif %}>gpt-4</option>
| |
| 83 | <option value="gpt-3.5-turbo" {% if model == 'gpt-3.5-turbo' %}selected{% endif %}>gpt-3.5-turbo</option>
| |
| 84 | <option value="gpt-3.5" {% if model == 'gpt-3.5' %}selected{% endif %}>gpt-3.5</option>
| |
| 85 | </select><br> | |
| 86 | System Message: <textarea name="system_message">{{ system_message }}</textarea><br>
| |
| 87 | User Message: <textarea name="user_message">{{ user_message }}</textarea><br>
| |
| 88 | Max Tokens: <input type="number" name="max_tokens" value="{{ max_tokens }}"><br>
| |
| 89 | Temperature: <input type="number" step="0.01" name="temperature" value="{{ temperature }}"><br>
| |
| 90 | <input type="submit" value="Send Request"> | |
| 91 | </form> | |
| 92 | {% if response %}
| |
| 93 | <div class="response"> | |
| 94 | <div class="response-header">Response ID: {{ response.id }}</div>
| |
| 95 | <div>Model: {{ response.model }}</div>
| |
| 96 | <div>Created: {{ response.created|datetimeformat }}</div>
| |
| 97 | <div class="response-content">{{ response.choices[0].message.content }}</div>
| |
| 98 | <div>Completion Reason: {{ response.choices[0].finish_reason }}</div>
| |
| 99 | <div>Token Usage: Prompt {{ response.usage.prompt_tokens }}, Completion {{ response.usage.completion_tokens }}, Total {{ response.usage.total_tokens }}</div>
| |
| 100 | </div> | |
| 101 | {% endif %}
| |
| 102 | </body> | |
| 103 | </html> | |
| 104 | """ | |
| 105 | ||
| 106 | @app.template_filter('datetimeformat')
| |
| 107 | def datetimeformat(value, format='%Y-%m-%d %H:%M:%S'): | |
| 108 | return datetime.utcfromtimestamp(value).strftime(format) | |
| 109 | ||
| 110 | @app.route('/', methods=['GET', 'POST'])
| |
| 111 | def index(): | |
| 112 | defaults = {
| |
| 113 | "api_key": "", | |
| 114 | "model": "gpt-4-turbo", | |
| 115 | "system_message": "Start a conversation about space adventures.", | |
| 116 | "user_message": "Tell me a story about a space adventure.", | |
| 117 | "max_tokens": 150, | |
| 118 | "temperature": 0.7, | |
| 119 | "response": None | |
| 120 | } | |
| 121 | ||
| 122 | if request.method == 'POST': | |
| 123 | defaults.update({
| |
| 124 | "api_key": request.form['api_key'], | |
| 125 | "model": request.form['model'], | |
| 126 | "system_message": request.form['system_message'], | |
| 127 | "user_message": request.form['user_message'], | |
| 128 | "max_tokens": int(request.form['max_tokens']), | |
| 129 | "temperature": float(request.form['temperature']) | |
| 130 | }) | |
| 131 | ||
| 132 | data = {
| |
| 133 | "model": defaults['model'], | |
| 134 | "messages": [ | |
| 135 | {"role": "system", "content": defaults['system_message']},
| |
| 136 | {"role": "user", "content": defaults['user_message']}
| |
| 137 | ], | |
| 138 | "max_tokens": defaults['max_tokens'], | |
| 139 | "temperature": defaults['temperature'] | |
| 140 | } | |
| 141 | headers = {
| |
| 142 | 'Authorization': f'Bearer {defaults["api_key"]}',
| |
| 143 | 'Content-Type': 'application/json' | |
| 144 | } | |
| 145 | url = "https://api.openai.com/v1/chat/completions" | |
| 146 | response = requests.post(url, headers=headers, data=json.dumps(data)) | |
| 147 | defaults['response'] = response.json() | |
| 148 | ||
| 149 | return render_template_string(HTML, **defaults) | |
| 150 | ||
| 151 | if __name__ == '__main__': | |
| 152 | app.run(debug=True) | |
| 153 |