Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

gradio_demo.py 4.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  1. import random
  2. import time
  3. import gradio as gr
  4. # Simple chatbot function
  5. def chatbot_response(message, history):
  6. """Simple chatbot that responds to messages"""
  7. time.sleep(1) # Simulate processing time
  8. responses = {
  9. "hello": "Hello! How can I help you today?",
  10. "how are you": "I'm doing great, thank you for asking! How are you?",
  11. "test": "Test successful! The browser automation is working correctly.",
  12. "demo": "This is a demo Gradio application for testing browser automation.",
  13. "help": "I can respond to simple greetings and questions. Try saying 'hello' or asking 'how are you'!",
  14. }
  15. # Simple keyword matching
  16. message_lower = message.lower()
  17. for keyword, response in responses.items():
  18. if keyword in message_lower:
  19. return response
  20. # Default response
  21. return f"I received your message: '{message}'. This is a simple demo response!"
  22. # Create the Gradio interface
  23. def create_demo():
  24. with gr.Blocks(title="Browser Testing Demo") as demo:
  25. gr.Markdown(
  26. """
  27. # 🤖 Browser Automation Test Demo
  28. This is a simple Gradio application designed for testing browser automation with promptfoo.
  29. **Features:**
  30. - Simple chat interface
  31. - Predictable responses for testing
  32. - Clear element identifiers for automation
  33. """
  34. )
  35. chatbot = gr.Chatbot(
  36. label="Chat History", elem_id="chat-history", type="messages"
  37. )
  38. msg = gr.Textbox(
  39. label="Your Message",
  40. placeholder="Type a message and press Enter...",
  41. elem_id="user-input",
  42. )
  43. with gr.Row():
  44. submit = gr.Button("Submit", elem_id="submit-button")
  45. clear = gr.Button("Clear", elem_id="clear-button")
  46. # Example messages for testing
  47. gr.Examples(
  48. examples=[
  49. "Hello",
  50. "How are you?",
  51. "This is a test",
  52. "Show me a demo",
  53. "Help",
  54. ],
  55. inputs=msg,
  56. label="Example Messages",
  57. )
  58. # Handle message submission
  59. def respond(message, chat_history):
  60. bot_message = chatbot_response(message, chat_history)
  61. # Use the new messages format with role and content
  62. chat_history.append({"role": "user", "content": message})
  63. chat_history.append({"role": "assistant", "content": bot_message})
  64. return "", chat_history
  65. # Wire up the interface
  66. msg.submit(respond, [msg, chatbot], [msg, chatbot])
  67. submit.click(respond, [msg, chatbot], [msg, chatbot])
  68. clear.click(lambda: None, None, chatbot, queue=False)
  69. return demo
  70. # Simple calculator for testing form inputs
  71. def create_calculator_demo():
  72. with gr.Blocks(title="Calculator Demo") as calc_demo:
  73. gr.Markdown("## Simple Calculator for Testing")
  74. with gr.Row():
  75. num1 = gr.Number(label="First Number", elem_id="num1")
  76. num2 = gr.Number(label="Second Number", elem_id="num2")
  77. operation = gr.Radio(
  78. ["Add", "Subtract", "Multiply", "Divide"],
  79. label="Operation",
  80. elem_id="operation",
  81. )
  82. calculate_btn = gr.Button("Calculate", elem_id="calculate")
  83. result = gr.Textbox(label="Result", elem_id="result")
  84. def calculate(n1, n2, op):
  85. if op == "Add":
  86. return str(n1 + n2)
  87. elif op == "Subtract":
  88. return str(n1 - n2)
  89. elif op == "Multiply":
  90. return str(n1 * n2)
  91. elif op == "Divide":
  92. return str(n1 / n2) if n2 != 0 else "Error: Division by zero"
  93. calculate_btn.click(calculate, [num1, num2, operation], result)
  94. return calc_demo
  95. if __name__ == "__main__":
  96. # Create a tabbed interface with multiple demos
  97. demo1 = create_demo()
  98. demo2 = create_calculator_demo()
  99. tabbed_demo = gr.TabbedInterface(
  100. [demo1, demo2],
  101. ["Chatbot Demo", "Calculator Demo"],
  102. title="Browser Automation Test Suite",
  103. )
  104. print("Starting Gradio demo server...")
  105. print("Access the demo at http://localhost:7860")
  106. print("Use Ctrl+C to stop the server")
  107. tabbed_demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...