Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
import langid | |
from googletrans import Translator | |
# Set up your OpenAI API credentials | |
openai.api_key = '' | |
# Initialize the translator | |
translator = Translator(service_urls=['translate.google.com']) | |
def translate_text(text, src_lang, dest_lang): | |
translated_text = translator.translate(text, src=src_lang, dest=dest_lang).text | |
return translated_text | |
def generate_answer(question): | |
input_lang = langid.classify(question)[0] | |
translated_question = translate_text(question, input_lang, 'en') | |
prompt = f"Give me the correct answer for this physics question: {translated_question}" | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=1025 | |
) | |
output_completion = response.choices[0].text.strip() | |
translated_output = translate_text(output_completion, 'en', input_lang) | |
return translated_output | |
iface = gr.Interface( | |
fn=generate_answer, | |
inputs=["text"], | |
outputs="text", | |
title="Physics AI", | |
description="Enter a physics question and get the correct answer.", | |
examples=[ | |
["What is the equation for calculating force?"], | |
["Define Newton's second law of motion."], | |
["What is the formula for calculating velocity?"], | |
] | |
) | |
iface.launch() | |