NikhilSetiya commited on
Commit
8d34c11
·
1 Parent(s): 96d9174

Add calculator app files for Hugging Face Spaces

Browse files
Files changed (7) hide show
  1. .gitattributes +5 -0
  2. .github/workflows/test.yml +31 -0
  3. README.md +19 -4
  4. app.py +4 -0
  5. frontend.py +7 -2
  6. requirements.txt +4 -2
  7. test_calculator.py +48 -0
.gitattributes ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ *.py linguist-language=Python
2
+ *.md linguist-language=Markdown
3
+ *.txt linguist-language=Text
4
+ *.yml linguist-language=YAML
5
+ *.yaml linguist-language=YAML
.github/workflows/test.yml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Python Tests
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ python-version: [3.9, 3.10]
15
+
16
+ steps:
17
+ - uses: actions/checkout@v3
18
+
19
+ - name: Set up Python ${{ matrix.python-version }}
20
+ uses: actions/setup-python@v4
21
+ with:
22
+ python-version: ${{ matrix.python-version }}
23
+
24
+ - name: Install dependencies
25
+ run: |
26
+ python -m pip install --upgrade pip
27
+ pip install -r requirements.txt
28
+
29
+ - name: Run tests
30
+ run: |
31
+ pytest test_calculator.py -v
README.md CHANGED
@@ -1,6 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
1
  # Calculator Application
2
 
3
- A modern calculator application built with FastAPI backend and Gradio frontend.
4
 
5
  ## Features
6
  - Addition
@@ -9,13 +20,17 @@ A modern calculator application built with FastAPI backend and Gradio frontend.
9
  - Division
10
  - Clean and intuitive user interface
11
  - Error handling for invalid operations
12
- - Division by zero protection
 
 
 
 
 
13
 
14
  ## Tech Stack
15
- - Backend: FastAPI
16
  - Frontend: Gradio
 
17
  - Data Validation: Pydantic
18
- - API Documentation: Swagger UI (automatically generated)
19
 
20
  ## Setup
21
 
 
1
+ ---
2
+ title: Calculator App
3
+ emoji: 🔢
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.12.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
  # Calculator Application
13
 
14
+ A simple calculator application built with Gradio and FastAPI.
15
 
16
  ## Features
17
  - Addition
 
20
  - Division
21
  - Clean and intuitive user interface
22
  - Error handling for invalid operations
23
+
24
+ ## How to Use
25
+ 1. Enter two numbers in the input fields
26
+ 2. Select the operation you want to perform
27
+ 3. Click the "Calculate" button
28
+ 4. View the result in the output field
29
 
30
  ## Tech Stack
 
31
  - Frontend: Gradio
32
+ - Backend: FastAPI
33
  - Data Validation: Pydantic
 
34
 
35
  ## Setup
36
 
app.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from frontend import demo
2
+
3
+ if __name__ == "__main__":
4
+ demo.launch()
frontend.py CHANGED
@@ -1,10 +1,14 @@
1
  import gradio as gr
2
  import requests
 
 
 
 
3
 
4
  def calculate(num1: float, num2: float, operation: str) -> str:
5
  try:
6
  response = requests.post(
7
- "http://localhost:8000/calculate",
8
  json={
9
  "num1": num1,
10
  "num2": num2,
@@ -41,5 +45,6 @@ with gr.Blocks(title="Calculator") as demo:
41
  outputs=result
42
  )
43
 
 
44
  if __name__ == "__main__":
45
- demo.launch()
 
1
  import gradio as gr
2
  import requests
3
+ import os
4
+
5
+ # Get the backend URL from environment variable or use localhost as fallback
6
+ BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
7
 
8
  def calculate(num1: float, num2: float, operation: str) -> str:
9
  try:
10
  response = requests.post(
11
+ f"{BACKEND_URL}/calculate",
12
  json={
13
  "num1": num1,
14
  "num2": num2,
 
45
  outputs=result
46
  )
47
 
48
+ # For Hugging Face Spaces
49
  if __name__ == "__main__":
50
+ demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt CHANGED
@@ -1,5 +1,7 @@
 
 
1
  fastapi==0.104.1
2
  uvicorn==0.24.0
3
  pydantic==2.5.2
4
- gradio==4.12.0
5
- requests==2.31.0
 
1
+ gradio==4.12.0
2
+ requests==2.31.0
3
  fastapi==0.104.1
4
  uvicorn==0.24.0
5
  pydantic==2.5.2
6
+ pytest==7.4.3
7
+ httpx==0.25.2
test_calculator.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from fastapi.testclient import TestClient
3
+ from backend import app
4
+
5
+ client = TestClient(app)
6
+
7
+ def test_addition():
8
+ response = client.post(
9
+ "/calculate",
10
+ json={"num1": 5, "num2": 3, "operation": "add"}
11
+ )
12
+ assert response.status_code == 200
13
+ assert response.json()["result"] == 8
14
+ assert response.json()["operation"] == "add"
15
+
16
+ def test_subtraction():
17
+ response = client.post(
18
+ "/calculate",
19
+ json={"num1": 5, "num2": 3, "operation": "subtract"}
20
+ )
21
+ assert response.status_code == 200
22
+ assert response.json()["result"] == 2
23
+ assert response.json()["operation"] == "subtract"
24
+
25
+ def test_multiplication():
26
+ response = client.post(
27
+ "/calculate",
28
+ json={"num1": 5, "num2": 3, "operation": "multiply"}
29
+ )
30
+ assert response.status_code == 200
31
+ assert response.json()["result"] == 15
32
+ assert response.json()["operation"] == "multiply"
33
+
34
+ def test_division():
35
+ response = client.post(
36
+ "/calculate",
37
+ json={"num1": 6, "num2": 2, "operation": "divide"}
38
+ )
39
+ assert response.status_code == 200
40
+ assert response.json()["result"] == 3
41
+ assert response.json()["operation"] == "divide"
42
+
43
+ def test_division_by_zero():
44
+ response = client.post(
45
+ "/calculate",
46
+ json={"num1": 5, "num2": 0, "operation": "divide"}
47
+ )
48
+ assert response.status_code == 422 # FastAPI returns 422 for validation errors