Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,28 @@
|
|
| 1 |
-
# PART 1: Import the necessary modules
|
| 2 |
-
from flask import Flask, render_template, request
|
| 3 |
import openai
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
api_key = "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"
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# PART A: Define the home route
|
| 12 |
-
@app.route('/')
|
| 13 |
-
def index():
|
| 14 |
-
# Render the index.html template with no question and result values
|
| 15 |
-
return render_template('index.html', question=None, result=None)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# PART B: Define the submit route
|
| 19 |
-
@app.route('/', methods=['POST'])
|
| 20 |
-
def submit():
|
| 21 |
-
# Retrieve the prompt from the form submission
|
| 22 |
-
prompt = request.form['prompt']
|
| 23 |
-
# Call the access_openai function passing in the prompt value
|
| 24 |
-
result = access_openai(prompt)
|
| 25 |
-
# Render the index.html template with the prompt and result values
|
| 26 |
-
return render_template('index.html', question=prompt, result=result)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Part C: Define the access_openai function
|
| 30 |
-
|
| 31 |
-
def access_openai(prompt_value):
|
| 32 |
-
openai.api_key = api_key
|
| 33 |
response = openai.Completion.create(
|
| 34 |
model="text-davinci-003",
|
| 35 |
-
prompt=f"請將以下的內容翻譯為英文:\n\n
|
| 36 |
max_tokens=200,
|
| 37 |
)
|
| 38 |
result = response.choices[0].text.strip()
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
+
# 設定你的 OpenAI API 金鑰
|
| 5 |
api_key = "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"
|
| 6 |
+
openai.api_key = api_key
|
| 7 |
|
| 8 |
+
# 定義呼叫 OpenAI API 的函數
|
| 9 |
+
def translate_text(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
response = openai.Completion.create(
|
| 11 |
model="text-davinci-003",
|
| 12 |
+
prompt=f"請將以下的內容翻譯為英文:\n\n{prompt}",
|
| 13 |
max_tokens=200,
|
| 14 |
)
|
| 15 |
result = response.choices[0].text.strip()
|
| 16 |
+
return result
|
| 17 |
+
|
| 18 |
+
# 使用 Gradio 建立 UI
|
| 19 |
+
interface = gr.Interface(
|
| 20 |
+
fn=translate_text,
|
| 21 |
+
inputs="text",
|
| 22 |
+
outputs="text",
|
| 23 |
+
title="中文翻譯器",
|
| 24 |
+
description="輸入中文句子,將其翻譯為英文。",
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# 啟動應用程式
|
| 28 |
+
interface.launch()
|