DasperMansfield commited on
Commit
9395e9e
·
verified ·
1 Parent(s): 0f227f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -132
app.py CHANGED
@@ -1,136 +1,44 @@
1
  import gradio as gr
2
- from gradio import ChatMessage
3
- import time
4
 
5
- def slow_echo(message, history):
6
- for i in range(len(message)):
7
- time.sleep(0.05)
8
- yield "You typed: " + message[: i + 1]
9
-
10
- demo = gr.ChatInterface(
11
- slow_echo,
12
- type="messages",
13
- flagging_mode="manual",
14
- flagging_options=["Like", "Spam", "Inappropriate", "Other"],
15
- save_history=True,
16
- )
17
-
18
-
19
- # Define the main function for the app
20
- def main_function(purpose, help_text, export_text, extra_menu_item, chat_input, tts_input, script_input):
21
- # Placeholder function to demonstrate the app
22
- return {
23
- "purpose_output": purpose,
24
- "help_output": help_text,
25
- "export_output": export_text,
26
- "extra_menu_output": extra_menu_item,
27
- "chat_output": chat_input,
28
- "tts_output": tts_input,
29
- "script_output": script_input
30
- }
31
-
32
- # Define the Gradio app
33
- with gr.Blocks() as demo:
34
- # Define input components
35
- purpose_input = gr.Textbox(label="Purpose")
36
- help_input = gr.Textbox(label="Help Text")
37
- export_input = gr.Textbox(label="Export Text")
38
- extra_menu_input = gr.Textbox(label="Extra Menu Item")
39
- chat_input = gr.Textbox(label="Chat Input")
40
- tts_input = gr.Textbox(label="TTS Input")
41
- script_input = gr.Textbox(label="Script Input")
42
-
43
- # Define output components
44
- purpose_output = gr.Textbox(label="Purpose Output")
45
- help_output = gr.Textbox(label="Help Output")
46
- export_output = gr.Textbox(label="Export Output")
47
- extra_menu_output = gr.Textbox(label="Extra Menu Output")
48
- chat_output = gr.Textbox(label="Chat Output")
49
- tts_output = gr.Textbox(label="TTS Output")
50
- script_output = gr.Textbox(label="Script Output")
51
-
52
- # Define the button to trigger the function
53
- submit_button = gr.Button("Submit", variant="primary")
54
-
55
- # Set up the event listener for the button
56
- submit_button.click(
57
- fn=main_function,
58
- inputs=[purpose_input, help_input, export_input, extra_menu_input, chat_input, tts_input, script_input],
59
- outputs=[purpose_output, help_output, export_output, extra_menu_output, chat_output, tts_output, script_output]
60
- )
61
-
62
- def generate_response(history):
63
- history.append(
64
- ChatMessage(
65
- role="user", content="What is the weather in San Francisco right now?"
66
- )
67
- )
68
- yield history
69
- time.sleep(0.25)
70
- history.append(
71
- ChatMessage(
72
- role="assistant",
73
- content="In order to find the current weather in San Francisco, I will need to use my weather tool.",
74
- )
75
- )
76
- yield history
77
- time.sleep(0.25)
78
-
79
- history.append(
80
- ChatMessage(
81
- role="assistant",
82
- content="API Error when connecting to weather service.",
83
- metadata={"title": "💥 Error using tool 'Weather'"},
84
- )
85
- )
86
- yield history
87
- time.sleep(0.25)
88
-
89
- history.append(
90
- ChatMessage(
91
- role="assistant",
92
- content="I will try again",
93
- )
94
- )
95
- yield history
96
- time.sleep(0.25)
97
-
98
- history.append(
99
- ChatMessage(
100
- role="assistant",
101
- content="Weather 72 degrees Fahrenheit with 20% chance of rain.",
102
- metadata={"title": "🛠️ Used tool 'Weather'"},
103
- )
104
- )
105
- yield history
106
- time.sleep(0.25)
107
-
108
- history.append(
109
- ChatMessage(
110
- role="assistant",
111
- content="Now that the API succeeded I can complete my task.",
112
- )
113
- )
114
- yield history
115
- time.sleep(0.25)
116
-
117
- history.append(
118
- ChatMessage(
119
- role="assistant",
120
- content="It's a sunny day in San Francisco with a current temperature of 72 degrees Fahrenheit and a 20% chance of rain. Enjoy the weather!",
121
- )
122
- )
123
- yield history
124
-
125
- def like(evt: gr.LikeData):
126
- print("User liked the response")
127
- print(evt.index, evt.liked, evt.value)
128
 
129
  with gr.Blocks() as demo:
130
- chatbot = gr.Chatbot(type="messages", height=500, show_copy_button=True)
131
- button = gr.Button("Get San Francisco Weather")
132
- button.click(generate_response, chatbot, chatbot)
133
- chatbot.like(like)
134
-
135
- if __name__ == "__main__":
136
- demo.launch()
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
2
 
3
+ python_code = """
4
+ def fib(n):
5
+ if n <= 0:
6
+ return 0
7
+ elif n == 1:
8
+ return 1
9
+ else:
10
+ return fib(n-1) + fib(n-2)
11
+ """
12
+
13
+ js_code = """
14
+ function fib(n) {
15
+ if (n <= 0) return 0;
16
+ if (n === 1) return 1;
17
+ return fib(n - 1) + fib(n - 2);
18
+ }
19
+ """
20
+
21
+ def chat(message, history):
22
+ if "python" in message.lower():
23
+ return "Type Python or JavaScript to see the code.", gr.Code(language="python", value=python_code)
24
+ elif "javascript" in message.lower():
25
+ return "Type Python or JavaScript to see the code.", gr.Code(language="javascript", value=js_code)
26
+ else:
27
+ return "Please ask about Python or JavaScript.", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  with gr.Blocks() as demo:
30
+ code = gr.Code(render=False)
31
+ with gr.Row():
32
+ with gr.Column():
33
+ gr.Markdown("<center><h1>Write Python or JavaScript</h1></center>")
34
+ gr.ChatInterface(
35
+ chat,
36
+ examples=["Python", "JavaScript"],
37
+ additional_outputs=[code],
38
+ type="messages"
39
+ )
40
+ with gr.Column():
41
+ gr.Markdown("<center><h1>Code Artifacts</h1></center>")
42
+ code.render()
43
+
44
+ demo.launch()