Onurcan Genç commited on
Commit ·
21eb127
1
Parent(s): bd1ed67
123
Browse files
app.py
CHANGED
|
@@ -1,62 +1,16 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import subprocess
|
| 6 |
-
import sys
|
| 7 |
-
import shlex
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
# Set
|
| 16 |
-
|
| 17 |
-
interpreter.llm.provider = "huggingface" # Specify the provider
|
| 18 |
-
interpreter.llm.model = "huggingface/gpt2" # Use an existing Hugging Face model identifier
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
interpreter.max_tokens = 1000
|
| 23 |
-
|
| 24 |
-
# Function to execute shell commands if detected in model output
|
| 25 |
-
def execute_shell_command(command):
|
| 26 |
-
# Sanitize user input using shlex.split to avoid security vulnerabilities
|
| 27 |
-
sanitized_command = shlex.split(command)
|
| 28 |
-
result = subprocess.run(sanitized_command, capture_output=True, text=True)
|
| 29 |
-
return result.stdout if result.returncode == 0 else result.stderr
|
| 30 |
-
|
| 31 |
-
# Function to process tasks using OpenInterpreter and detect shell commands
|
| 32 |
-
def execute_task(task):
|
| 33 |
-
try:
|
| 34 |
-
# Get the model-generated response first
|
| 35 |
-
response = "".join([chunk if isinstance(chunk, str) else str(chunk) for chunk in interpreter.chat(task, stream=True)])
|
| 36 |
-
except Exception as e:
|
| 37 |
-
return f"Error while interacting with the model: {str(e)}"
|
| 38 |
-
|
| 39 |
-
# Check if the response indicates a shell command
|
| 40 |
-
if "run shell" in response.lower():
|
| 41 |
-
command = response.split("run shell", 1)[1].strip()
|
| 42 |
-
return execute_shell_command(command)
|
| 43 |
-
else:
|
| 44 |
-
return response
|
| 45 |
-
|
| 46 |
-
# CLI interface using argparse
|
| 47 |
-
def cli_interface():
|
| 48 |
-
parser = argparse.ArgumentParser(description="Command-line interaction with OpenInterpreter.")
|
| 49 |
-
parser.add_argument("--task", type=str, help="The task or command to execute")
|
| 50 |
-
args = parser.parse_args()
|
| 51 |
-
|
| 52 |
-
# Provide a default task if none is provided
|
| 53 |
-
task = args.task if args.task else "Tell me a joke"
|
| 54 |
-
|
| 55 |
-
# Execute the task and print the result
|
| 56 |
-
result = execute_task(task)
|
| 57 |
-
print(result)
|
| 58 |
-
|
| 59 |
-
# Main entry point for the script
|
| 60 |
-
if __name__ == "__main__":
|
| 61 |
-
# Run the CLI interface only, no web app
|
| 62 |
-
cli_interface()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load the model
|
| 6 |
+
generator = pipeline("text-generation", model="gpt-neo-2.7B")
|
| 7 |
|
| 8 |
+
# Define a function to handle input and generate text
|
| 9 |
+
def generate_text(prompt):
|
| 10 |
+
return generator(prompt, max_length=100, do_sample=True)[0]["generated_text"]
|
| 11 |
|
| 12 |
+
# Set up Gradio interface
|
| 13 |
+
interface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Launch the app
|
| 16 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|