File size: 2,566 Bytes
f8adf83 8a33ed0 8544107 db36d92 8a33ed0 8544107 41d37a9 8544107 5f738f9 8544107 e8bd651 5f738f9 e8bd651 b076c07 2e060bc b076c07 4d6ff6c b076c07 41d37a9 bcae335 5339d88 73dc865 f8adf83 8a33ed0 f8adf83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import gradio as gr
import openai
# Set up your OpenAI API key
openai.api_key = ""
# Define the function to handle the user's inputs and generate the output
def process_quote(quote, action, target_language):
if action == "find author":
# Call OpenAI API to find the author of the quote
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Find the author of the quote: '{quote}'",
max_tokens=30,
n=1,
stop=None,
temperature=0.7
)
author = response.choices[0].text.strip()
return f"The author of the quote '{quote}' is {author}."
elif action == "explain quote":
# Call OpenAI API to explain the quote
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Explain the quote: '{quote}'",
max_tokens=100,
n=1,
stop=None,
temperature=0.7
)
explanation = response.choices[0].text.strip()
return f"Explanation of the quote '{quote}': {explanation}."
elif action == "generate similar quote":
# Call OpenAI API to generate a similar quote with fewer tokens
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"This is similar quote that I have generated:",
max_tokens=30,
n=1,
stop=None,
temperature=0.7
)
similar_quote = response.choices[0].text.strip()
return f"A similar quote to '{quote}': {similar_quote}."
elif action == "translate quote":
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"The translation of the following quote in English is: '{quote}'",
max_tokens=100,
n=1,
stop=None,
temperature=0.7
)
translation = response.choices[0].text.strip()
return f"Translation of the quote '{quote}': {translation}."
quote_input = gr.inputs.Textbox(label="Enter a quote")
action_input = gr.inputs.Dropdown(["find author", "explain quote", "generate similar quote", "translate quote"], label="Select an action")
language_input = gr.inputs.Dropdown(["en", "fr", "es", "de"], label="Select target language (for translation)")
output_text = gr.outputs.Textbox(label="Output")
interface = gr.Interface(fn=process_quote, inputs=[quote_input, action_input, language_input], outputs=[output_text])
# Run the interface
interface.launch()
|