Onurcan Genç commited on
Commit ·
d07fe3b
1
Parent(s): 94594e8
new
Browse files- app.py +49 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from interpreter import interpreter
|
| 4 |
+
import argparse
|
| 5 |
+
import subprocess
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
# Fetch the Hugging Face API key from environment variables
|
| 9 |
+
api_key = os.getenv("HUGGINGFACE_API_KEY")
|
| 10 |
+
|
| 11 |
+
if not api_key:
|
| 12 |
+
raise ValueError("API key not found. Set the HUGGINGFACE_API_KEY environment variable.")
|
| 13 |
+
|
| 14 |
+
# Set the API key for the interpreter
|
| 15 |
+
interpreter.llm.api_key = api_key
|
| 16 |
+
interpreter.llm.model = "meta-llama/Llama-3.1-8B-Instruct" # Example model
|
| 17 |
+
|
| 18 |
+
# Function to execute tasks using OpenInterpreter
|
| 19 |
+
def execute_task(task):
|
| 20 |
+
if task.startswith("run shell"):
|
| 21 |
+
command = task.replace("run shell", "").strip()
|
| 22 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
| 23 |
+
return result.stdout if result.returncode == 0 else result.stderr
|
| 24 |
+
else:
|
| 25 |
+
return interpreter.chat(task)
|
| 26 |
+
|
| 27 |
+
# CLI interface using argparse
|
| 28 |
+
def cli_interface():
|
| 29 |
+
parser = argparse.ArgumentParser(description="Command-line interaction with OpenInterpreter.")
|
| 30 |
+
parser.add_argument("--task", type=str, required=True, help="The task or command to execute")
|
| 31 |
+
args = parser.parse_args()
|
| 32 |
+
|
| 33 |
+
# Execute the task and print the result
|
| 34 |
+
result = execute_task(args.task)
|
| 35 |
+
print(result)
|
| 36 |
+
|
| 37 |
+
# Gradio interface for the web app
|
| 38 |
+
def run_gradio_interface():
|
| 39 |
+
iface = gr.Interface(fn=execute_task, inputs="text", outputs="text")
|
| 40 |
+
iface.launch()
|
| 41 |
+
|
| 42 |
+
# Main entry point for the script
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
if len(sys.argv) > 1:
|
| 45 |
+
# Run the CLI if arguments are provided
|
| 46 |
+
cli_interface()
|
| 47 |
+
else:
|
| 48 |
+
# Run the Gradio web app by default
|
| 49 |
+
run_gradio_interface()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.1
|
| 2 |
+
open-interpreter
|