|
import gradio as gr |
|
import openai |
|
|
|
|
|
openai.api_key = "" |
|
|
|
|
|
def process_quote(quote, action, target_language): |
|
if action == "find author": |
|
|
|
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": |
|
|
|
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": |
|
|
|
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]) |
|
|
|
|
|
interface.launch() |
|
|