File size: 1,149 Bytes
51857fd
f276882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51857fd
 
 
 
1878582
f276882
1878582
 
 
f276882
 
6667ce7
 
 
 
1878582
51857fd
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
import gradio
import subprocess

def run_command(command):
    try:
        result = subprocess.run(
            command,
            shell=True,
            check=True,
            text=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            input=None  # You can provide input to the command here as a string
        )
        return result.stdout.strip()
    except subprocess.CalledProcessError as e:
        return f"Error: {e}"
    except Exception as e:
        return f"An error occurred: {e}"

# Example usage


def my_inference_function(name):
  return "Hello " + name + "!"

gradio_interface = gradio.Interface(
  fn=run_command,
  inputs="text",
  outputs="text",
  examples=[
    ["ls -l"],
    ["pip install bs4"]
  ],
  title="REST API with Gradio and Huggingface Spaces",
  description="This is a demo of how to build an AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
  article="© Tom Söderlund 2022"
)
gradio_interface.launch()