SaintlyPioneer commited on
Commit
06ebaee
·
1 Parent(s): 4e61d69

add chat ui as well in gradio

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -28,8 +28,7 @@ def calculate(num1: float, num2: float, operation: Literal["add", "subtract", "m
28
  except ValueError as e:
29
  return str(e)
30
 
31
-
32
- def compute(num1: float, num2: float, operation: str):
33
  import httpx
34
 
35
  # Extract the base URL from the request
@@ -43,8 +42,29 @@ def compute(num1: float, num2: float, operation: str):
43
  else:
44
  return
45
 
46
- iface = gr.Interface(fn=compute, inputs=[gr.Number("First Number"), gr.Number("Second Number"), gr.Dropdown(choices=["add", "subtract", "multiply", "divide"])], outputs="text", title="Calculator")
47
- app = gr.mount_gradio_app(app, iface, '/')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  if __name__ == "__main__":
50
  import uvicorn
 
28
  except ValueError as e:
29
  return str(e)
30
 
31
+ def makeRequest(num1: float, num2: float, operation: str):
 
32
  import httpx
33
 
34
  # Extract the base URL from the request
 
42
  else:
43
  return
44
 
45
+ def compute(num1: float, num2: float, operation: str):
46
+ return makeRequest(num1, num2, operation)
47
+
48
+ def chatCompute(message, _):
49
+ import re
50
+ num1, operand, num2 = re.match(r"(\d+)\s*([+\-*/])\s*(\d+)", message).groups()
51
+ operation = {
52
+ "+": "add",
53
+ "-": "subtract",
54
+ "*": "multiply",
55
+ "/": "divide"
56
+ }[operand]
57
+ return makeRequest(num1, num2, operation)
58
+
59
+ iface1 = gr.Interface(fn=compute, inputs=[gr.Number("First Number"), gr.Number("Second Number"), gr.Dropdown(choices=["add", "subtract", "multiply", "divide"])], outputs="text", title="Calculator")
60
+ iface2 = gr.ChatInterface(chatCompute, title="Calculator")
61
+
62
+ # Combine them using TabbedInterface
63
+ tabbed_interface = gr.TabbedInterface(
64
+ interface_list=[iface1, iface2],
65
+ tab_names=["Calculator", "Chat Calculator"]
66
+ )
67
+ app = gr.mount_gradio_app(app, tabbed_interface, '/')
68
 
69
  if __name__ == "__main__":
70
  import uvicorn