DcChamp / app.py
Shubhy's picture
Update app.py
db36d92
raw
history blame contribute delete
No virus
2.57 kB
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()