dragonhearted commited on
Commit
01d6799
Β·
1 Parent(s): 473df84

upload files

Browse files
Files changed (5) hide show
  1. .gitignore +174 -0
  2. README.md +1 -1
  3. api.py +26 -0
  4. requirements.txt +5 -0
  5. ui.py +81 -0
.gitignore ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # VSCode
2
+ .vscode/*
3
+ !.vscode/settings.json
4
+ !.vscode/tasks.json
5
+ !.vscode/launch.json
6
+ !.vscode/extensions.json
7
+ *.code-workspace
8
+ # Local History for Visual Studio Code
9
+ .history/
10
+
11
+ # Common credential files
12
+ **/credentials.json
13
+ **/client_secrets.json
14
+ **/client_secret.json
15
+ *creds*
16
+ *.dat
17
+ *password*
18
+ *.httr-oauth*
19
+
20
+ # Private Node Modules
21
+ node_modules/
22
+ creds.js
23
+
24
+ # Private Files
25
+ *.json
26
+ *.csv
27
+ *.csv.gz
28
+ *.tsv
29
+ *.tsv.gz
30
+ *.xlsx
31
+
32
+
33
+ # Mac/OSX
34
+ .DS_Store
35
+
36
+
37
+ # Byte-compiled / optimized / DLL files
38
+ __pycache__/
39
+ *.py[cod]
40
+ *$py.class
41
+
42
+ # C extensions
43
+ *.so
44
+
45
+ # Distribution / packaging
46
+ .Python
47
+ build/
48
+ develop-eggs/
49
+ dist/
50
+ downloads/
51
+ eggs/
52
+ .eggs/
53
+ lib/
54
+ lib64/
55
+ parts/
56
+ sdist/
57
+ var/
58
+ wheels/
59
+ share/python-wheels/
60
+ *.egg-info/
61
+ .installed.cfg
62
+ *.egg
63
+ MANIFEST
64
+
65
+ # PyInstaller
66
+ # Usually these files are written by a python script from a template
67
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
68
+ *.manifest
69
+ *.spec
70
+
71
+ # Installer logs
72
+ pip-log.txt
73
+ pip-delete-this-directory.txt
74
+
75
+ # Unit test / coverage reports
76
+ htmlcov/
77
+ .tox/
78
+ .nox/
79
+ .coverage
80
+ .coverage.*
81
+ .cache
82
+ nosetests.xml
83
+ coverage.xml
84
+ *.cover
85
+ *.py,cover
86
+ .hypothesis/
87
+ .pytest_cache/
88
+ cover/
89
+
90
+ # Translations
91
+ *.mo
92
+ *.pot
93
+
94
+ # Django stuff:
95
+ *.log
96
+ local_settings.py
97
+ db.sqlite3
98
+ db.sqlite3-journal
99
+
100
+ # Flask stuff:
101
+ instance/
102
+ .webassets-cache
103
+
104
+ # Scrapy stuff:
105
+ .scrapy
106
+
107
+ # Sphinx documentation
108
+ docs/_build/
109
+
110
+ # PyBuilder
111
+ .pybuilder/
112
+ target/
113
+
114
+ # Jupyter Notebook
115
+ .ipynb_checkpoints
116
+
117
+ # IPython
118
+ profile_default/
119
+ ipython_config.py
120
+
121
+ # pyenv
122
+ # For a library or package, you might want to ignore these files since the code is
123
+ # intended to run in multiple environments; otherwise, check them in:
124
+ # .python-version
125
+
126
+ # pipenv
127
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
128
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
129
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
130
+ # install all needed dependencies.
131
+ #Pipfile.lock
132
+
133
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
134
+ __pypackages__/
135
+
136
+ # Celery stuff
137
+ celerybeat-schedule
138
+ celerybeat.pid
139
+
140
+ # SageMath parsed files
141
+ *.sage.py
142
+
143
+ # Environments
144
+ .env
145
+ .venv
146
+ env/
147
+ venv/
148
+ ENV/
149
+ env.bak/
150
+ venv.bak/
151
+
152
+ # Spyder project settings
153
+ .spyderproject
154
+ .spyproject
155
+
156
+ # Rope project settings
157
+ .ropeproject
158
+
159
+ # mkdocs documentation
160
+ /site
161
+
162
+ # mypy
163
+ .mypy_cache/
164
+ .dmypy.json
165
+ dmypy.json
166
+
167
+ # Pyre type checker
168
+ .pyre/
169
+
170
+ # pytype static type analyzer
171
+ .pytype/
172
+
173
+ # Cython debug symbols
174
+ cython_debug/
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Calculator Api
3
- emoji: 😻
4
  colorFrom: indigo
5
  colorTo: indigo
6
  sdk: gradio
 
1
  ---
2
  title: Calculator Api
3
+ emoji: πžΉπŸ“ˆπŸ§ πŸ“š
4
  colorFrom: indigo
5
  colorTo: indigo
6
  sdk: gradio
api.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import uvicorn
4
+
5
+ app = FastAPI()
6
+
7
+ class Inputs(BaseModel):
8
+ expression: str
9
+
10
+ def calculate(expression):
11
+ try:
12
+ result = eval(expression)
13
+ return result
14
+ except Exception as e:
15
+ raise Exception("ERROR")
16
+
17
+ @app.post("/calculate")
18
+ async def calculate_endpoint(request: Inputs):
19
+ try:
20
+ result = calculate(request.expression)
21
+ return {'result': result}
22
+ except Exception as e:
23
+ return {"error": str(e)}
24
+
25
+ if __name__ == "__main__":
26
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.111.1
2
+ gradio==4.39.0
3
+ pydantic==2.8.2
4
+ requests==2.32.3
5
+ uvicorn==0.30.3
ui.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import re
4
+
5
+ def calculate(expression):
6
+ response = requests.post("http://localhost:8000/calculate", json={"expression": expression})
7
+ result = response.json()
8
+ if "error" in result:
9
+ return result["error"]
10
+ return result["result"]
11
+
12
+ def clear_display():
13
+ return ""
14
+
15
+ def append_symbol(current_expression, symbol):
16
+ return current_expression + symbol
17
+
18
+ def validate_expression(expression):
19
+ if expression == "ERROR":
20
+ return expression
21
+ valid_expression = re.sub(r'[^0-9+\-*/.]', '', expression)
22
+ return valid_expression
23
+
24
+ with gr.Blocks() as demo:
25
+ display = gr.Textbox(max_lines=1, value="", label="Expression", interactive=True)
26
+ display.change(fn=validate_expression, inputs=display, outputs=display)
27
+
28
+ with gr.Row():
29
+ gr.Button("AC", variant="primary", scale=3, elem_classes=["btn-AC"]).click(clear_display, outputs=display)
30
+ gr.Button("+", variant="primary", scale=1, elem_classes=["btn"]).click(lambda x: append_symbol(x, "+"), inputs=display, outputs=display)
31
+
32
+ with gr.Row():
33
+ for symbol in ["7", "8", "9"]:
34
+ gr.Button(symbol, elem_classes=["btn"]).click(lambda x, s=symbol: append_symbol(x, s), inputs=display, outputs=display)
35
+ gr.Button("-", variant="primary", elem_classes=["btn"]).click(lambda x: append_symbol(x, "-"), inputs=display, outputs=display)
36
+
37
+ with gr.Row():
38
+ for symbol in ["4", "5", "6"]:
39
+ gr.Button(symbol, elem_classes=["btn"]).click(lambda x, s=symbol: append_symbol(x, s), inputs=display, outputs=display)
40
+ gr.Button("x", variant="primary", elem_classes=["btn"]).click(lambda x: append_symbol(x, "*"), inputs=display, outputs=display)
41
+
42
+ with gr.Row():
43
+ for symbol in ["1", "2", "3"]:
44
+ gr.Button(symbol, elem_classes=["btn"]).click(lambda x, s=symbol: append_symbol(x, s), inputs=display, outputs=display)
45
+ gr.Button("/", variant="primary", elem_classes=["btn"]).click(lambda x: append_symbol(x, "/"), inputs=display, outputs=display)
46
+
47
+ with gr.Row():
48
+ for symbol in ["0", "."]:
49
+ gr.Button(symbol, elem_classes=["btn"]).click(lambda x, s=symbol: append_symbol(x, s), inputs=display, outputs=display)
50
+ gr.Button("=", variant="primary", scale=2, elem_classes=["btn-equalto"]).click(calculate, inputs=display, outputs=display)
51
+ display.submit(calculate, inputs=display, outputs=display)
52
+
53
+ demo.css = '''
54
+ body, .gradio-container {
55
+ display: flex;
56
+ justify-content: center;
57
+ align-items: center;
58
+ height: 100vh;
59
+ }
60
+ .gradio-container {
61
+ flex-direction: column;
62
+ width: auto;
63
+ }
64
+ .btn {
65
+ font-size: 1.5rem;
66
+ min-width: 5rem;
67
+ max-width: 6rem;
68
+ }
69
+ .btn-AC{
70
+ font-size: 1.5rem;
71
+ min-width: 5rem;
72
+ max-width: 20rem;
73
+ }
74
+ .btn-equalto{
75
+ font-size: 1.5rem;
76
+ min-width: 5rem;
77
+ max-width: 13rem;
78
+ }
79
+ '''
80
+
81
+ demo.launch()