Spaces:
Sleeping
Sleeping
Ifeanyi
commited on
Commit
·
a688180
1
Parent(s):
fcbc412
Initial commit
Browse files- Dockerfile +34 -0
- app.py +64 -0
Dockerfile
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python 3.10 base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set environment variables
|
| 5 |
+
ENV R_HOME=/usr/lib/R
|
| 6 |
+
ENV PATH="${R_HOME}/bin:${PATH}"
|
| 7 |
+
|
| 8 |
+
# Set the working directory in the container
|
| 9 |
+
WORKDIR /app
|
| 10 |
+
|
| 11 |
+
# Install system dependencies and R
|
| 12 |
+
RUN apt-get update && \
|
| 13 |
+
apt-get install -y --no-install-recommends \
|
| 14 |
+
r-base \
|
| 15 |
+
libcurl4-openssl-dev \
|
| 16 |
+
libssl-dev \
|
| 17 |
+
libxml2-dev \
|
| 18 |
+
libgit2-dev && \
|
| 19 |
+
rm -rf /var/lib/apt/lists/*
|
| 20 |
+
|
| 21 |
+
# Copy the requirements file into the container
|
| 22 |
+
COPY requirements.txt .
|
| 23 |
+
|
| 24 |
+
# Install Python dependencies
|
| 25 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 26 |
+
|
| 27 |
+
# Copy the rest of the application code into the container
|
| 28 |
+
COPY . .
|
| 29 |
+
|
| 30 |
+
# Expose the port that Gradio will run on
|
| 31 |
+
EXPOSE 7860
|
| 32 |
+
|
| 33 |
+
# Command to run the application
|
| 34 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import rpy2.robjects as robj
|
| 3 |
+
from rpy2.robjects import default_converter
|
| 4 |
+
from rpy2.robjects.conversion import localconverter
|
| 5 |
+
|
| 6 |
+
# define R functions globally
|
| 7 |
+
robj.r("""
|
| 8 |
+
add <- function(x,y) { return(x + y) }
|
| 9 |
+
multiply <- function(x,y) { return(x * y) }
|
| 10 |
+
sub <- function(x,y) { return(x - y) }
|
| 11 |
+
div <- function(x,y) { return(x / y) }
|
| 12 |
+
""")
|
| 13 |
+
|
| 14 |
+
def arithmetic(num1, num2, operation):
|
| 15 |
+
|
| 16 |
+
with localconverter(default_converter):
|
| 17 |
+
|
| 18 |
+
if operation == "Add":
|
| 19 |
+
res = robj.r(f"add({num1}, {num2})")
|
| 20 |
+
elif operation == "Multiply":
|
| 21 |
+
res = robj.r(f"multiply({num1}, {num2})")
|
| 22 |
+
elif operation == "Subtract":
|
| 23 |
+
res = robj.r(f"sub({num1}, {num2})")
|
| 24 |
+
elif operation == "Divide":
|
| 25 |
+
res = robj.r(f"div({num1}, {num2})")
|
| 26 |
+
|
| 27 |
+
return res[0]
|
| 28 |
+
|
| 29 |
+
with gr.Blocks(
|
| 30 |
+
title="R-Powered Calculator",
|
| 31 |
+
theme="upsatwal/mlsc_tiet",
|
| 32 |
+
css = "footer {display: none !important;}"
|
| 33 |
+
) as app:
|
| 34 |
+
gr.HTML(
|
| 35 |
+
"""
|
| 36 |
+
<h1 style='text-align: center'>
|
| 37 |
+
<strong>R-Powered Calculator</strong>
|
| 38 |
+
</h1>
|
| 39 |
+
"""
|
| 40 |
+
)
|
| 41 |
+
with gr.Row():
|
| 42 |
+
num1 = gr.Number(label="Number 1")
|
| 43 |
+
num2 = gr.Number(label="Number 2")
|
| 44 |
+
with gr.Column():
|
| 45 |
+
operation = gr.Radio(choices=["Add", "Multiply", "Subtract", "Divide"],
|
| 46 |
+
value="Add",
|
| 47 |
+
label="Choose Arithmetic Operation",
|
| 48 |
+
min_width=100,
|
| 49 |
+
scale=0.3)
|
| 50 |
+
with gr.Row():
|
| 51 |
+
submit = gr.Button("Run Operation")
|
| 52 |
+
with gr.Column():
|
| 53 |
+
gr.HTML(
|
| 54 |
+
"""
|
| 55 |
+
<h3 style='text-align: center'>
|
| 56 |
+
<strong>Arithmetic Operation Result</strong>
|
| 57 |
+
</h3>
|
| 58 |
+
"""
|
| 59 |
+
)
|
| 60 |
+
result = gr.Number()
|
| 61 |
+
|
| 62 |
+
submit.click(arithmetic, inputs=[num1, num2, operation], outputs=[result])
|
| 63 |
+
|
| 64 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|