Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162# -*- coding: utf-8 -*-import gradio as grimport google.generativeai as genai# Configure the API keygenai.configure(api_key='AIzaSyArybMiPDZARDCz7yZBjzEMx6zXgOXHtoc')# Define the completion function with error handlingdef get_completion(code_snippet):try:python_code_examples = """---------------------Example 1: x = 10def foo():global xx = 5foo()print(x)Correct output: 5Code Explanation: The global keyword modifies the global variable x to be 5.---------------------Example 2: def modify_list(input_list):input_list.append(4)input_list = [1, 2, 3]my_list = [0]modify_list(my_list)print(my_list)Correct output: [0, 4]Code Explanation: Appending 4 modifies the input_list but does not affect the original my_list.---------------------"""prompt = f"""Your task is to explain the following code snippet step-by-step:{python_code_examples}```{code_snippet}```"""# Use the correct API method to generate textresponse = genai.generate_text(prompt=prompt,temperature=0,max_output_tokens=500,)return response.result # Access the result of the responseexcept Exception as e:# Return the error messagereturn f"An error occurred: {str(e)}"# Create the Gradio interfaceiface = gr.Interface(fn=get_completion,inputs=gr.Textbox(label="Insert Code Snippet", lines=5),outputs=gr.Textbox(label="Explanation Here", lines=8),title="Code Explainer")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263# -*- coding: utf-8 -*-import gradio as grimport openaiimport os# Configure the OpenAI API keyopenai.api_key = os.getenv('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') # Ensure your API key is set in environment variablesdef get_completion(code_snippet):# Code examples to illustrate the outputpython_code_examples = """---------------------Example 1:x = 10def foo():global xx = 5foo()print(x)Correct output: 5Explanation: The global keyword allows foo() to modify the global variable x.---------------------Example 2:def modify_list(input_list):input_list.append(4)input_list = [1, 2, 3]my_list = [0]modify_list(my_list)print(my_list)Correct output: [0, 4]Explanation: The function appends 4 to input_list, which modifies my_list directly.---------------------"""prompt = f"""Your task is to explain the following code snippet step-by-step, using the examples below as reference.{python_code_examples}Code Snippet:```{code_snippet}```"""try:completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", # Specify the modelmessages=[{"role": "user", "content": prompt}],max_tokens=500)return completion.choices[0].message['content']except Exception as e:return f"An error occurred: {str(e)}"# Define the Gradio interfaceiface = gr.Interface(fn=get_completion,inputs=[gr.Textbox(label="Insert Code Snippet", lines=5)],outputs=[gr.Textbox(label="Explanation Here", lines=8)],title="Code Explainer")
app.py+47-46