Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,63 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
-
# Configure the API key
|
| 7 |
-
|
| 8 |
|
| 9 |
-
# Define the completion function with error handling
|
| 10 |
def get_completion(code_snippet):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
Example 2: def modify_list(input_list):
|
| 24 |
-
input_list.append(4)
|
| 25 |
-
input_list = [1, 2, 3]
|
| 26 |
-
my_list = [0]
|
| 27 |
-
modify_list(my_list)
|
| 28 |
-
print(my_list)
|
| 29 |
-
Correct output: [0, 4]
|
| 30 |
-
Code Explanation: Appending 4 modifies the input_list but does not affect the original my_list.
|
| 31 |
-
---------------------
|
| 32 |
-
"""
|
| 33 |
-
|
| 34 |
-
prompt = f"""
|
| 35 |
-
Your task is to explain the following code snippet step-by-step:
|
| 36 |
-
{python_code_examples}
|
| 37 |
-
```
|
| 38 |
-
{code_snippet}
|
| 39 |
-
```
|
| 40 |
-
"""
|
| 41 |
-
|
| 42 |
-
# Use the correct API method to generate text
|
| 43 |
-
response = genai.generate_text(
|
| 44 |
-
prompt=prompt,
|
| 45 |
-
temperature=0,
|
| 46 |
-
max_output_tokens=500,
|
| 47 |
-
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
# Return the error message
|
| 53 |
return f"An error occurred: {str(e)}"
|
| 54 |
|
| 55 |
-
#
|
| 56 |
iface = gr.Interface(
|
| 57 |
fn=get_completion,
|
| 58 |
-
inputs=gr.Textbox(label="Insert Code Snippet", lines=5),
|
| 59 |
-
outputs=gr.Textbox(label="Explanation Here", lines=8),
|
| 60 |
title="Code Explainer"
|
| 61 |
)
|
| 62 |
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
import openai
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Configure the OpenAI API key
|
| 8 |
+
openai.api_key = os.getenv('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') # Ensure your API key is set in environment variables
|
| 9 |
|
|
|
|
| 10 |
def get_completion(code_snippet):
|
| 11 |
+
# Code examples to illustrate the output
|
| 12 |
+
python_code_examples = """
|
| 13 |
+
---------------------
|
| 14 |
+
Example 1:
|
| 15 |
+
x = 10
|
| 16 |
+
def foo():
|
| 17 |
+
global x
|
| 18 |
+
x = 5
|
| 19 |
+
foo()
|
| 20 |
+
print(x)
|
| 21 |
+
Correct output: 5
|
| 22 |
+
Explanation: The global keyword allows foo() to modify the global variable x.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
---------------------
|
| 25 |
+
Example 2:
|
| 26 |
+
def modify_list(input_list):
|
| 27 |
+
input_list.append(4)
|
| 28 |
+
input_list = [1, 2, 3]
|
| 29 |
+
my_list = [0]
|
| 30 |
+
modify_list(my_list)
|
| 31 |
+
print(my_list)
|
| 32 |
+
Correct output: [0, 4]
|
| 33 |
+
Explanation: The function appends 4 to input_list, which modifies my_list directly.
|
| 34 |
+
---------------------
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
prompt = f"""
|
| 38 |
+
Your task is to explain the following code snippet step-by-step, using the examples below as reference.
|
| 39 |
+
{python_code_examples}
|
| 40 |
+
Code Snippet:
|
| 41 |
+
```
|
| 42 |
+
{code_snippet}
|
| 43 |
+
```
|
| 44 |
+
"""
|
| 45 |
|
| 46 |
+
try:
|
| 47 |
+
completion = openai.ChatCompletion.create(
|
| 48 |
+
model="gpt-3.5-turbo", # Specify the model
|
| 49 |
+
messages=[{"role": "user", "content": prompt}],
|
| 50 |
+
max_tokens=500
|
| 51 |
+
)
|
| 52 |
+
return completion.choices[0].message['content']
|
| 53 |
except Exception as e:
|
|
|
|
| 54 |
return f"An error occurred: {str(e)}"
|
| 55 |
|
| 56 |
+
# Define the Gradio interface
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=get_completion,
|
| 59 |
+
inputs=[gr.Textbox(label="Insert Code Snippet", lines=5)],
|
| 60 |
+
outputs=[gr.Textbox(label="Explanation Here", lines=8)],
|
| 61 |
title="Code Explainer"
|
| 62 |
)
|
| 63 |
|