tharun1507 commited on
Commit
9797e14
·
verified ·
1 Parent(s): f781e59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -54
app.py CHANGED
@@ -1,63 +1,82 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
38
 
39
- response += token
40
- yield response
 
 
 
41
 
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
+ # -*- coding: utf-8 -*-
2
+ """Copy of Code Explainer.ipynb
3
+ Automatically generated by Colaboratory.
4
+ Original file is located at
5
+ https://colab.research.google.com/drive/1tBrZ0R8BVUoBwSSkv07CMTrXnsYKmeXI
6
  """
 
7
 
8
 
9
+ #@title Code Explainer
10
+ import gradio as gr
11
+ import google.generativeai as palm
 
 
 
 
 
 
12
 
13
+ # load model
14
+ # PaLM API Key here
15
+ palm.configure(api_key='AIzaSyArybMiPDZARDCz7yZBjzEMx6zXgOXHtoc')
16
+ # Use the palm.list_models function to find available models
17
+ # PaLM 2 available in 4 sizes: Gecko, Otter, Bison and Unicorn (largest)
18
+ models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
19
+ model = models[0].name
20
+ # define completion function
21
+ def get_completion(code_snippet):
22
 
23
+ python_code_examples = f"""
24
+ ---------------------
25
+ Example 1: Code Snippet
26
+ x = 10
27
+ def foo():
28
+ global x
29
+ x = 5
30
+ foo()
31
+ print(x)
32
+ Correct output: 5
33
+ Code Explanation: Inside the foo function, the global keyword is used to modify the global variable x to be 5.
34
+ So, print(x) outside the function prints the modified value, which is 5.
35
+ ---------------------
36
+ Example 2: Code Snippet
37
+ def modify_list(input_list):
38
+ input_list.append(4)
39
+ input_list = [1, 2, 3]
40
+ my_list = [0]
41
+ modify_list(my_list)
42
+ print(my_list)
43
+ Correct output: [0, 4]
44
+ Code Explanation: Inside the modify_list function, an element 4 is appended to input_list.
45
+ Then, input_list is reassigned to a new list [1, 2, 3], but this change doesn't affect the original list.
46
+ So, print(my_list) outputs [0, 4].
47
+ ---------------------
48
+ """
49
 
50
+ prompt = f"""
51
+ Your task is to act as any language Code Explainer.
52
+ I'll give you a Code Snippet.
53
+ Your job is to explain the Code Snippet step-by-step.
54
+ Break down the code into as many steps as possible.
55
+ Share intermediate checkpoints & steps along with results.
56
+ Few good examples of Python code output between #### separator:
57
+ ####
58
+ {python_code_examples}
59
+ ####
60
+ Code Snippet is shared below, delimited with triple backticks:
61
+ ```
62
+ {code_snippet}
63
+ ```
64
+ """
65
 
66
+ completion = palm.generate_text(
67
+ model=model,
68
+ prompt=prompt,
69
+ temperature=0,
70
+ # The maximum length of the response
71
+ max_output_tokens=500,
72
+ )
73
+ response = completion.result
74
+ return response
75
 
76
+ # define app UI
77
+ iface = gr.Interface(fn=get_completion, inputs=[gr.Textbox(label="Insert Code Snippet",lines=5)],
78
+ outputs=[gr.Textbox(label="Explanation Here",lines=8)],
79
+ title="DECIPHER \n The Python Code Explainer \n AI Capstone Project(XII-C) "
80
+ )
81
 
82
+ iface.launch()