|
import gradio as gr |
|
import os |
|
import json |
|
from pathlib import Path |
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer |
|
import logging |
|
import hashlib |
|
|
|
|
|
logging.basicConfig(filename='remokode.log', level=logging.INFO) |
|
|
|
|
|
model_name = "distilbert-base-uncased" |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
def chatbot(message): |
|
""" |
|
Handles user input and responds with a relevant message |
|
""" |
|
try: |
|
inputs = tokenizer(message, return_tensors="pt") |
|
outputs = model(**inputs) |
|
response = tokenizer.decode(outputs.logits.argmax(-1), skip_special_tokens=True) |
|
return response |
|
except Exception as e: |
|
logging.error(f"Error in chatbot: {e}") |
|
return "Error: unable to process input" |
|
|
|
|
|
def terminal(command): |
|
""" |
|
Executes a terminal command and returns the output |
|
""" |
|
try: |
|
|
|
if not command.strip(): |
|
return "Error: invalid command" |
|
|
|
output = os.popen(command).read() |
|
return output |
|
except Exception as e: |
|
logging.error(f"Error in terminal: {e}") |
|
return "Error: unable to execute command" |
|
|
|
|
|
def explorer(path): |
|
""" |
|
Returns a list of files and directories in the given path |
|
""" |
|
try: |
|
|
|
if not path.strip(): |
|
return "Error: invalid path" |
|
|
|
files = [] |
|
for file in Path(path).iterdir(): |
|
files.append(file.name) |
|
return json.dumps(files) |
|
except Exception as e: |
|
logging.error(f"Error in explorer: {e}") |
|
return "Error: unable to access path" |
|
|
|
|
|
def package_manager(command): |
|
""" |
|
Manages packages and abilities for the chat app |
|
""" |
|
try: |
|
|
|
if not command.strip(): |
|
return "Error: invalid command" |
|
|
|
if command == "list": |
|
return "List of packages: [...]" |
|
elif command == "install": |
|
return "Package installed successfully" |
|
else: |
|
return "Error: invalid package manager command" |
|
except Exception as e: |
|
logging.error(f"Error in package manager: {e}") |
|
return "Error: unable to execute package manager command" |
|
|
|
|
|
def authenticate(username, password): |
|
""" |
|
Authenticates the user and returns a session token |
|
""" |
|
try: |
|
|
|
if not username.strip() or not password.strip(): |
|
return "Error: invalid username or password" |
|
|
|
|
|
session_token = hashlib.sha256(f"{username}:{password}".encode()).hexdigest() |
|
return session_token |
|
except Exception as e: |
|
logging.error(f"Error in authentication: {e}") |
|
return "Error: unable to authenticate user" |
|
|
|
|
|
def manage_session(session_token): |
|
""" |
|
Manages the user session and returns the session state |
|
""" |
|
try: |
|
|
|
if not session_token.strip(): |
|
return "Error: invalid session token" |
|
|
|
|
|
session_state = {"username": "user", "packages": ["package1", "package2"]} |
|
return session_state |
|
except Exception as e: |
|
logging.error(f"Error in session management: {e}") |
|
return "Error: unable to manage session" |
|
|
|
|
|
demo = gr.Interface( |
|
fn=chatbot, |
|
inputs="textbox", |
|
outputs="textbox", |
|
title="Remokode Chat App", |
|
description="A dev space chat app with terminal and in-app-explorer" |
|
) |
|
|
|
|
|
terminal_component = gr.components.Textbox(label="Terminal") |
|
demo.add_component(terminal_component, inputs="textbox", outputs="textbox", fn=terminal) |
|
|
|
|
|
explorer_component = gr.components.FileBrowser(label="In-App Explorer") |
|
demo.add_component(explorer_component, inputs=None, outputs="json", fn=explorer) |
|
|
|
|
|
package_manager_component = gr.components.Textbox(label="Package Manager") |
|
demo.add_component(package_manager_component, inputs="textbox", outputs="textbox", fn=package_manager) |
|
|
|
|
|
authentication_component = gr.components.Textbox(label="Username") |
|
password_component = gr.components.Textbox(label="Password", type="password") |
|
demo.add_component(authentication_component, inputs=[authentication_component, password_component], outputs="textbox", fn=authenticate) |
|
|
|
|
|
session_component = gr.components.Textbox(label="Session Token") |
|
demo.add_component(session_component, inputs=[session_component], outputs="textbox", fn=manage_session) |
|
|
|
|
|
demo.launch(share=True) |