tharun1507 commited on
Commit
4408f66
·
verified ·
1 Parent(s): 06721a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -42
app.py CHANGED
@@ -3,69 +3,38 @@
3
  import gradio as gr
4
  import google.generativeai as genai
5
 
6
- # Configure the API key
7
- genai.configure(api_key='AIzaSyYourGenAIKeyHere')
8
 
9
- # Define the completion function using the unified generate_content method
10
  def get_completion(code_snippet):
11
  python_code_examples = """
12
  ---------------------
13
- Example 1: Code Snippet
14
- x = 10
15
- def foo():
16
- global x
17
- x = 5
18
- foo()
19
- print(x)
20
- Correct output: 5
21
- Code Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.
22
- So, print(x) outside the function prints the modified value, which is 5.
23
- ---------------------
24
- Example 2: Code Snippet
25
- def modify_list(input_list):
26
- input_list.append(4)
27
- input_list = [1, 2, 3]
28
- my_list = [0]
29
- modify_list(my_list)
30
- print(my_list)
31
- Correct output: [0, 4]
32
- Code Explanation: Inside the modify_list function, an element 4 is appended to input_list.
33
- Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.
34
- So, print(my_list) outputs [0, 4].
35
  ---------------------
36
  """
37
-
38
  prompt = f"""
39
- Your task is to act as any language Code Explainer.
40
- I'll give you a Code Snippet.
41
- Your job is to explain the Code Snippet step-by-step.
42
- Break down the code into as many steps as possible.
43
- Share intermediate checkpoints & steps along with results.
44
- Few good examples of Python code output between #### separator:
45
- ####
46
  {python_code_examples}
47
- ####
48
- Code Snippet is shared below, delimited with triple backticks:
49
  ```
50
  {code_snippet}
51
  ```
52
  """
53
 
54
- # Generate content using the unified method
55
- completion = genai.generate_content(
56
  prompt=prompt,
57
  temperature=0,
58
  max_output_tokens=500,
59
  )
60
 
61
- response = completion.result
62
- return response
63
 
64
- # Define the Gradio app UI
65
  iface = gr.Interface(
66
  fn=get_completion,
67
- inputs=[gr.Textbox(label="Insert Code Snippet", lines=5)],
68
- outputs=[gr.Textbox(label="Explanation Here", lines=8)],
69
  title="Code Explainer"
70
  )
71
 
 
3
  import gradio as gr
4
  import google.generativeai as genai
5
 
6
+ # Configure API key
7
+ genai.configure(api_key='AIzaSyArybMiPDZARDCz7yZBjzEMx6zXgOXHtoc')
8
 
9
+ # Define the completion function using the new API methods
10
  def get_completion(code_snippet):
11
  python_code_examples = """
12
  ---------------------
13
+ Example 1: x = 10 ... (your code examples)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  ---------------------
15
  """
 
16
  prompt = f"""
17
+ Your task is to explain the following code snippet step-by-step:
 
 
 
 
 
 
18
  {python_code_examples}
 
 
19
  ```
20
  {code_snippet}
21
  ```
22
  """
23
 
24
+ # Generate text using the new method
25
+ response = genai.generate_content(
26
  prompt=prompt,
27
  temperature=0,
28
  max_output_tokens=500,
29
  )
30
 
31
+ return response.text
 
32
 
33
+ # Create the Gradio interface
34
  iface = gr.Interface(
35
  fn=get_completion,
36
+ inputs=gr.Textbox(label="Insert Code Snippet", lines=5),
37
+ outputs=gr.Textbox(label="Explanation Here", lines=8),
38
  title="Code Explainer"
39
  )
40