Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,25 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
-
import random
|
| 5 |
-
import string
|
| 6 |
-
from huggingface_hub import cached_download, hf_hub_url
|
| 7 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 8 |
import black
|
| 9 |
-
import
|
| 10 |
-
from
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Define functions for each feature
|
| 15 |
|
|
@@ -23,22 +33,36 @@ def chat_interface(input_text):
|
|
| 23 |
Returns:
|
| 24 |
The chatbot's response.
|
| 25 |
"""
|
| 26 |
-
# Load the
|
| 27 |
-
model_name =
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Generate chatbot response
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
return response
|
| 35 |
|
|
|
|
| 36 |
# 2. Terminal
|
| 37 |
-
def terminal_interface(command):
|
| 38 |
"""Executes commands in the terminal.
|
| 39 |
|
| 40 |
Args:
|
| 41 |
command: User's command.
|
|
|
|
| 42 |
|
| 43 |
Returns:
|
| 44 |
The terminal output.
|
|
@@ -47,10 +71,18 @@ def terminal_interface(command):
|
|
| 47 |
try:
|
| 48 |
process = subprocess.run(command.split(), capture_output=True, text=True)
|
| 49 |
output = process.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
output = f
|
| 52 |
return output
|
| 53 |
|
|
|
|
| 54 |
# 3. Code Editor
|
| 55 |
def code_editor_interface(code):
|
| 56 |
"""Provides code completion, formatting, and linting in the code editor.
|
|
@@ -69,14 +101,19 @@ def code_editor_interface(code):
|
|
| 69 |
|
| 70 |
# Lint code using pylint
|
| 71 |
try:
|
| 72 |
-
pylint_output =
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
lint_message = f"Pylint error: {e}"
|
| 77 |
|
| 78 |
return formatted_code, lint_message
|
| 79 |
|
|
|
|
| 80 |
# 4. Workspace
|
| 81 |
def workspace_interface(project_name):
|
| 82 |
"""Manages projects, files, and resources in the workspace.
|
|
@@ -87,14 +124,43 @@ def workspace_interface(project_name):
|
|
| 87 |
Returns:
|
| 88 |
Project creation status.
|
| 89 |
"""
|
|
|
|
| 90 |
# Create project directory
|
| 91 |
try:
|
| 92 |
-
os.makedirs(
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
except FileExistsError:
|
| 95 |
-
status = f'Project
|
| 96 |
return status
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# 5. AI-Infused Tools
|
| 99 |
|
| 100 |
# Define custom AI-powered tools using Hugging Face models
|
|
@@ -109,13 +175,78 @@ def summarize_text(text):
|
|
| 109 |
Returns:
|
| 110 |
Summarized text.
|
| 111 |
"""
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
return summary
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# 6. Code Generation
|
| 117 |
def generate_code(idea):
|
| 118 |
-
"""Generates code based on a given idea using the
|
| 119 |
|
| 120 |
Args:
|
| 121 |
idea: The idea for the code to be generated.
|
|
@@ -125,9 +256,12 @@ def generate_code(idea):
|
|
| 125 |
"""
|
| 126 |
|
| 127 |
# Load the code generation model
|
| 128 |
-
model_name =
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Generate the code
|
| 133 |
input_text = f"""
|
|
@@ -151,119 +285,202 @@ def generate_code(idea):
|
|
| 151 |
|
| 152 |
return generated_code
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
|
| 158 |
Args:
|
| 159 |
-
text:
|
| 160 |
|
| 161 |
Returns:
|
| 162 |
-
|
| 163 |
"""
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 167 |
-
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 168 |
-
result = classifier(text)[0]
|
| 169 |
-
return result
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
translation = translator(text, target_lang=target_language)[0]['translation_text']
|
| 184 |
-
return translation
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
st.sidebar.header("Select Workspace")
|
| 191 |
-
project_name = st.sidebar.selectbox("Choose a project", os.listdir('projects'))
|
| 192 |
-
|
| 193 |
-
# Chat Interface
|
| 194 |
-
st.header("Chat with CodeCraft")
|
| 195 |
-
chat_input = st.text_area("Enter your message:")
|
| 196 |
-
if st.button("Send"):
|
| 197 |
-
chat_response = chat_interface(chat_input)
|
| 198 |
-
st.write(f"CodeCraft: {chat_response}")
|
| 199 |
-
|
| 200 |
-
# Terminal Interface
|
| 201 |
-
st.header("Terminal")
|
| 202 |
-
terminal_input = st.text_input("Enter a command:")
|
| 203 |
-
if st.button("Run"):
|
| 204 |
-
terminal_output = terminal_interface(terminal_input)
|
| 205 |
-
st.code(terminal_output, language="bash")
|
| 206 |
-
|
| 207 |
-
# Code Editor Interface
|
| 208 |
-
st.header("Code Editor")
|
| 209 |
-
code_editor = st.text_area("Write your code:", language="python", height=300)
|
| 210 |
-
if st.button("Format & Lint"):
|
| 211 |
-
formatted_code, lint_message = code_editor_interface(code_editor)
|
| 212 |
-
st.code(formatted_code, language="python")
|
| 213 |
-
st.info(lint_message)
|
| 214 |
-
|
| 215 |
-
# AI-Infused Tools
|
| 216 |
-
st.header("AI-Powered Tools")
|
| 217 |
-
|
| 218 |
-
# Text Summarization
|
| 219 |
-
st.subheader("Text Summarization")
|
| 220 |
-
text_to_summarize = st.text_area("Enter text to summarize:")
|
| 221 |
-
if st.button("Summarize"):
|
| 222 |
-
summary = summarize_text(text_to_summarize)
|
| 223 |
-
st.write(f"Summary: {summary}")
|
| 224 |
-
|
| 225 |
-
# Sentiment Analysis
|
| 226 |
-
st.subheader("Sentiment Analysis")
|
| 227 |
-
text_to_analyze = st.text_area("Enter text to analyze sentiment:")
|
| 228 |
-
if st.button("Analyze Sentiment"):
|
| 229 |
-
sentiment_result = analyze_sentiment(text_to_analyze)
|
| 230 |
-
st.write(f"Sentiment: {sentiment_result['label']}, Score: {sentiment_result['score']}")
|
| 231 |
-
|
| 232 |
-
# Text Translation
|
| 233 |
-
st.subheader("Text Translation")
|
| 234 |
-
text_to_translate = st.text_area("Enter text to translate:")
|
| 235 |
-
target_language = st.selectbox("Choose target language", ['fr', 'es', 'de', 'zh-CN']) # Example languages
|
| 236 |
-
if st.button("Translate"):
|
| 237 |
-
translation = translate_text(text_to_translate, target_language)
|
| 238 |
-
st.write(f"Translation: {translation}")
|
| 239 |
-
|
| 240 |
-
# Code Generation
|
| 241 |
-
st.header("Code Generation")
|
| 242 |
-
code_idea = st.text_input("Enter your code idea:")
|
| 243 |
-
if st.button("Generate Code"):
|
| 244 |
-
try:
|
| 245 |
-
generated_code = generate_code(code_idea)
|
| 246 |
-
st.code(generated_code, language="python")
|
| 247 |
-
except Exception as e:
|
| 248 |
-
st.error(f"Error generating code: {e}")
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
|
| 255 |
-
|
| 256 |
-
hf_token = st.text_input("Enter your Hugging Face Token:")
|
| 257 |
-
if hf_token:
|
| 258 |
-
# Set the token using HfFolder
|
| 259 |
-
HfFolder.save_token(hf_token)
|
| 260 |
|
| 261 |
-
|
| 262 |
-
command = f"cd projects/{project_name} && streamlit run chat_app.py"
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import subprocess
|
|
|
|
|
|
|
|
|
|
| 4 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import black
|
| 6 |
+
from pylint import lint
|
| 7 |
+
from io import StringIO
|
| 8 |
+
import openai
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
# Set your OpenAI API key here
|
| 12 |
+
openai.api_key = "YOUR_OPENAI_API_KEY"
|
| 13 |
+
|
| 14 |
+
PROJECT_ROOT = "projects"
|
| 15 |
+
|
| 16 |
+
# Global state to manage communication between Tool Box and Workspace Chat App
|
| 17 |
+
if 'chat_history' not in st.session_state:
|
| 18 |
+
st.session_state.chat_history = []
|
| 19 |
+
if 'terminal_history' not in st.session_state:
|
| 20 |
+
st.session_state.terminal_history = []
|
| 21 |
+
if 'workspace_projects' not in st.session_state:
|
| 22 |
+
st.session_state.workspace_projects = {}
|
| 23 |
|
| 24 |
# Define functions for each feature
|
| 25 |
|
|
|
|
| 33 |
Returns:
|
| 34 |
The chatbot's response.
|
| 35 |
"""
|
| 36 |
+
# Load the GPT-2 model which is compatible with AutoModelForCausalLM
|
| 37 |
+
model_name = "gpt2"
|
| 38 |
+
try:
|
| 39 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 41 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 42 |
+
except EnvironmentError as e:
|
| 43 |
+
return f"Error loading model: {e}"
|
| 44 |
+
|
| 45 |
+
# Truncate input text to avoid exceeding the model's maximum length
|
| 46 |
+
max_input_length = 900
|
| 47 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 48 |
+
if input_ids.shape[1] > max_input_length:
|
| 49 |
+
input_ids = input_ids[:, :max_input_length]
|
| 50 |
|
| 51 |
# Generate chatbot response
|
| 52 |
+
outputs = model.generate(
|
| 53 |
+
input_ids, max_new_tokens=50, num_return_sequences=1, do_sample=True
|
| 54 |
+
)
|
| 55 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 56 |
return response
|
| 57 |
|
| 58 |
+
|
| 59 |
# 2. Terminal
|
| 60 |
+
def terminal_interface(command, project_name=None):
|
| 61 |
"""Executes commands in the terminal.
|
| 62 |
|
| 63 |
Args:
|
| 64 |
command: User's command.
|
| 65 |
+
project_name: Name of the project workspace to add installed packages.
|
| 66 |
|
| 67 |
Returns:
|
| 68 |
The terminal output.
|
|
|
|
| 71 |
try:
|
| 72 |
process = subprocess.run(command.split(), capture_output=True, text=True)
|
| 73 |
output = process.stdout
|
| 74 |
+
|
| 75 |
+
# If the command is to install a package, update the workspace
|
| 76 |
+
if "install" in command and project_name:
|
| 77 |
+
requirements_path = os.path.join(PROJECT_ROOT, project_name, "requirements.txt")
|
| 78 |
+
with open(requirements_path, "a") as req_file:
|
| 79 |
+
package_name = command.split()[-1]
|
| 80 |
+
req_file.write(f"{package_name}\n")
|
| 81 |
except Exception as e:
|
| 82 |
+
output = f"Error: {e}"
|
| 83 |
return output
|
| 84 |
|
| 85 |
+
|
| 86 |
# 3. Code Editor
|
| 87 |
def code_editor_interface(code):
|
| 88 |
"""Provides code completion, formatting, and linting in the code editor.
|
|
|
|
| 101 |
|
| 102 |
# Lint code using pylint
|
| 103 |
try:
|
| 104 |
+
pylint_output = StringIO()
|
| 105 |
+
sys.stdout = pylint_output
|
| 106 |
+
sys.stderr = pylint_output
|
| 107 |
+
lint.Run(['--from-stdin'], stdin=StringIO(formatted_code))
|
| 108 |
+
sys.stdout = sys.__stdout__
|
| 109 |
+
sys.stderr = sys.__stderr__
|
| 110 |
+
lint_message = pylint_output.getvalue()
|
| 111 |
except Exception as e:
|
| 112 |
lint_message = f"Pylint error: {e}"
|
| 113 |
|
| 114 |
return formatted_code, lint_message
|
| 115 |
|
| 116 |
+
|
| 117 |
# 4. Workspace
|
| 118 |
def workspace_interface(project_name):
|
| 119 |
"""Manages projects, files, and resources in the workspace.
|
|
|
|
| 124 |
Returns:
|
| 125 |
Project creation status.
|
| 126 |
"""
|
| 127 |
+
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 128 |
# Create project directory
|
| 129 |
try:
|
| 130 |
+
os.makedirs(project_path)
|
| 131 |
+
requirements_path = os.path.join(project_path, "requirements.txt")
|
| 132 |
+
with open(requirements_path, "w") as req_file:
|
| 133 |
+
req_file.write("") # Initialize an empty requirements.txt file
|
| 134 |
+
status = f'Project "{project_name}" created successfully.'
|
| 135 |
+
st.session_state.workspace_projects[project_name] = {'files': []}
|
| 136 |
except FileExistsError:
|
| 137 |
+
status = f'Project "{project_name}" already exists.'
|
| 138 |
return status
|
| 139 |
|
| 140 |
+
def add_code_to_workspace(project_name, code, file_name):
|
| 141 |
+
"""Adds selected code files to the workspace.
|
| 142 |
+
|
| 143 |
+
Args:
|
| 144 |
+
project_name: Name of the project.
|
| 145 |
+
code: Code to be added.
|
| 146 |
+
file_name: Name of the file to be created.
|
| 147 |
+
|
| 148 |
+
Returns:
|
| 149 |
+
File creation status.
|
| 150 |
+
"""
|
| 151 |
+
project_path = os.path.join(PROJECT_ROOT, project_name)
|
| 152 |
+
file_path = os.path.join(project_path, file_name)
|
| 153 |
+
|
| 154 |
+
try:
|
| 155 |
+
with open(file_path, "w") as code_file:
|
| 156 |
+
code_file.write(code)
|
| 157 |
+
status = f'File "{file_name}" added to project "{project_name}" successfully.'
|
| 158 |
+
st.session_state.workspace_projects[project_name]['files'].append(file_name)
|
| 159 |
+
except Exception as e:
|
| 160 |
+
status = f"Error: {e}"
|
| 161 |
+
return status
|
| 162 |
+
|
| 163 |
+
|
| 164 |
# 5. AI-Infused Tools
|
| 165 |
|
| 166 |
# Define custom AI-powered tools using Hugging Face models
|
|
|
|
| 175 |
Returns:
|
| 176 |
Summarized text.
|
| 177 |
"""
|
| 178 |
+
# Load the summarization model
|
| 179 |
+
model_name = "facebook/bart-large-cnn"
|
| 180 |
+
try:
|
| 181 |
+
summarizer = pipeline("summarization", model=model_name)
|
| 182 |
+
except EnvironmentError as e:
|
| 183 |
+
return f"Error loading model: {e}"
|
| 184 |
+
|
| 185 |
+
# Truncate input text to avoid exceeding the model's maximum length
|
| 186 |
+
max_input_length = 1024
|
| 187 |
+
inputs = text
|
| 188 |
+
if len(text) > max_input_length:
|
| 189 |
+
inputs = text[:max_input_length]
|
| 190 |
+
|
| 191 |
+
# Generate summary
|
| 192 |
+
summary = summarizer(inputs, max_length=100, min_length=30, do_sample=False)[0][
|
| 193 |
+
"summary_text"
|
| 194 |
+
]
|
| 195 |
return summary
|
| 196 |
|
| 197 |
+
# Example: Sentiment analysis tool
|
| 198 |
+
def sentiment_analysis(text):
|
| 199 |
+
"""Performs sentiment analysis on a given text using a Hugging Face model.
|
| 200 |
+
|
| 201 |
+
Args:
|
| 202 |
+
text: Text to be analyzed.
|
| 203 |
+
|
| 204 |
+
Returns:
|
| 205 |
+
Sentiment analysis result.
|
| 206 |
+
"""
|
| 207 |
+
# Load the sentiment analysis model
|
| 208 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 209 |
+
try:
|
| 210 |
+
analyzer = pipeline("sentiment-analysis", model=model_name)
|
| 211 |
+
except EnvironmentError as e:
|
| 212 |
+
return f"Error loading model: {e}"
|
| 213 |
+
|
| 214 |
+
# Perform sentiment analysis
|
| 215 |
+
result = analyzer(text)[0]
|
| 216 |
+
return result
|
| 217 |
+
|
| 218 |
+
# Example: Text translation tool (code translation)
|
| 219 |
+
def translate_code(code, source_language, target_language):
|
| 220 |
+
"""Translates code from one programming language to another using OpenAI Codex.
|
| 221 |
+
|
| 222 |
+
Args:
|
| 223 |
+
code: Code to be translated.
|
| 224 |
+
source_language: The source programming language.
|
| 225 |
+
target_language: The target programming language.
|
| 226 |
+
|
| 227 |
+
Returns:
|
| 228 |
+
Translated code.
|
| 229 |
+
"""
|
| 230 |
+
prompt = f"Translate the following {source_language} code to {target_language}:\n\n{code}"
|
| 231 |
+
try:
|
| 232 |
+
response = openai.Completion.create(
|
| 233 |
+
engine="code-davinci-002",
|
| 234 |
+
prompt=prompt,
|
| 235 |
+
max_tokens=1024,
|
| 236 |
+
temperature=0.3,
|
| 237 |
+
top_p=1,
|
| 238 |
+
n=1,
|
| 239 |
+
stop=None
|
| 240 |
+
)
|
| 241 |
+
translated_code = response.choices[0].text.strip()
|
| 242 |
+
except Exception as e:
|
| 243 |
+
translated_code = f"Error: {e}"
|
| 244 |
+
return translated_code
|
| 245 |
+
|
| 246 |
+
|
| 247 |
# 6. Code Generation
|
| 248 |
def generate_code(idea):
|
| 249 |
+
"""Generates code based on a given idea using the EleutherAI/gpt-neo-2.7B model.
|
| 250 |
|
| 251 |
Args:
|
| 252 |
idea: The idea for the code to be generated.
|
|
|
|
| 256 |
"""
|
| 257 |
|
| 258 |
# Load the code generation model
|
| 259 |
+
model_name = "EleutherAI/gpt-neo-2.7B"
|
| 260 |
+
try:
|
| 261 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 262 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 263 |
+
except EnvironmentError as e:
|
| 264 |
+
return f"Error loading model: {e}"
|
| 265 |
|
| 266 |
# Generate the code
|
| 267 |
input_text = f"""
|
|
|
|
| 285 |
|
| 286 |
return generated_code
|
| 287 |
|
| 288 |
+
|
| 289 |
+
# 7. AI Personas Creator
|
| 290 |
+
def create_persona_from_text(text):
|
| 291 |
+
"""Creates an AI persona from the given text.
|
| 292 |
|
| 293 |
Args:
|
| 294 |
+
text: Text to be used for creating the persona.
|
| 295 |
|
| 296 |
Returns:
|
| 297 |
+
Persona prompt.
|
| 298 |
"""
|
| 299 |
+
persona_prompt = f"""
|
| 300 |
+
As an elite expert developer with the highest level of proficiency in Streamlit, Gradio, and Hugging Face, I possess a comprehensive understanding of these technologies and their applications in web development and deployment. My expertise encompasses the following areas:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
+
Streamlit:
|
| 303 |
+
* In-depth knowledge of Streamlit's architecture, components, and customization options.
|
| 304 |
+
* Expertise in creating interactive and user-friendly dashboards and applications.
|
| 305 |
+
* Proficiency in integrating Streamlit with various data sources and machine learning models.
|
| 306 |
|
| 307 |
+
Gradio:
|
| 308 |
+
* Thorough understanding of Gradio's capabilities for building and deploying machine learning interfaces.
|
| 309 |
+
* Expertise in creating custom Gradio components and integrating them with Streamlit applications.
|
| 310 |
+
* Proficiency in using Gradio to deploy models from Hugging Face and other frameworks.
|
| 311 |
|
| 312 |
+
Hugging Face:
|
| 313 |
+
* Comprehensive knowledge of Hugging Face's model hub and Transformers library.
|
| 314 |
+
* Expertise in fine-tuning and deploying Hugging Face models for various NLP and computer vision tasks.
|
| 315 |
+
* Proficiency in using Hugging Face's Spaces platform for model deployment and sharing.
|
|
|
|
|
|
|
| 316 |
|
| 317 |
+
Deployment:
|
| 318 |
+
* In-depth understanding of best practices for deploying Streamlit and Gradio applications.
|
| 319 |
+
* Expertise in deploying models on cloud platforms such as AWS, Azure, and GCP.
|
| 320 |
+
* Proficiency in optimizing deployment configurations for performance and scalability.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
+
Additional Skills:
|
| 323 |
+
* Strong programming skills in Python and JavaScript.
|
| 324 |
+
* Familiarity with Docker and containerization technologies.
|
| 325 |
+
* Excellent communication and problem-solving abilities.
|
| 326 |
|
| 327 |
+
I am confident that I can leverage my expertise to assist you in developing and deploying cutting-edge web applications using Streamlit, Gradio, and Hugging Face. Please feel free to ask any questions or present any challenges you may encounter.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
+
Example:
|
|
|
|
| 330 |
|
| 331 |
+
Task:
|
| 332 |
+
Develop a Streamlit application that allows users to generate text using a Hugging Face model. The application should include a Gradio component for user input and model prediction.
|
| 333 |
+
|
| 334 |
+
Solution:
|
| 335 |
+
|
| 336 |
+
import streamlit as st
|
| 337 |
+
import gradio as gr
|
| 338 |
+
from transformers import pipeline
|
| 339 |
+
|
| 340 |
+
# Create a Hugging Face pipeline
|
| 341 |
+
huggingface_model = pipeline("text-generation")
|
| 342 |
+
|
| 343 |
+
# Create a Streamlit app
|
| 344 |
+
st.title("Hugging Face Text Generation App")
|
| 345 |
+
|
| 346 |
+
# Define a Gradio component
|
| 347 |
+
demo = gr.Interface(
|
| 348 |
+
fn=huggingface_model,
|
| 349 |
+
inputs=gr.Textbox(lines=2),
|
| 350 |
+
outputs=gr.Textbox(lines=1),
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
# Display the Gradio component in the Streamlit app
|
| 354 |
+
st.write(demo)
|
| 355 |
+
"""
|
| 356 |
+
return persona_prompt
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
# Streamlit App
|
| 360 |
+
st.title("AI Personas Creator")
|
| 361 |
+
|
| 362 |
+
# Sidebar navigation
|
| 363 |
+
st.sidebar.title("Navigation")
|
| 364 |
+
app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Personas Creator", "Tool Box", "Workspace Chat App"])
|
| 365 |
+
|
| 366 |
+
if app_mode == "AI Personas Creator":
|
| 367 |
+
# AI Personas Creator
|
| 368 |
+
st.header("Create the System Prompt of an AI Persona from YouTube or Text")
|
| 369 |
+
|
| 370 |
+
st.subheader("From Text")
|
| 371 |
+
text_input = st.text_area("Enter text to create an AI persona:")
|
| 372 |
+
if st.button("Create Persona"):
|
| 373 |
+
persona_prompt = create_persona_from_text(text_input)
|
| 374 |
+
st.subheader("Persona Prompt")
|
| 375 |
+
st.text_area("You may now copy the text below and use it as Custom prompt!", value=persona_prompt, height=300)
|
| 376 |
+
|
| 377 |
+
elif app_mode == "Tool Box":
|
| 378 |
+
# Tool Box
|
| 379 |
+
st.header("AI-Powered Tools")
|
| 380 |
+
|
| 381 |
+
# Chat Interface
|
| 382 |
+
st.subheader("Chat with CodeCraft")
|
| 383 |
+
chat_input = st.text_area("Enter your message:")
|
| 384 |
+
if st.button("Send"):
|
| 385 |
+
chat_response = chat_interface(chat_input)
|
| 386 |
+
st.session_state.chat_history.append((chat_input, chat_response))
|
| 387 |
+
st.write(f"CodeCraft: {chat_response}")
|
| 388 |
+
|
| 389 |
+
# Terminal Interface
|
| 390 |
+
st.subheader("Terminal")
|
| 391 |
+
terminal_input = st.text_input("Enter a command:")
|
| 392 |
+
if st.button("Run"):
|
| 393 |
+
terminal_output = terminal_interface(terminal_input)
|
| 394 |
+
st.session_state.terminal_history.append((terminal_input, terminal_output))
|
| 395 |
+
st.code(terminal_output, language="bash")
|
| 396 |
+
|
| 397 |
+
# Code Editor Interface
|
| 398 |
+
st.subheader("Code Editor")
|
| 399 |
+
code_editor = st.text_area("Write your code:", height=300)
|
| 400 |
+
if st.button("Format & Lint"):
|
| 401 |
+
formatted_code, lint_message = code_editor_interface(code_editor)
|
| 402 |
+
st.code(formatted_code, language="python")
|
| 403 |
+
st.info(lint_message)
|
| 404 |
+
|
| 405 |
+
# Text Summarization Tool
|
| 406 |
+
st.subheader("Summarize Text")
|
| 407 |
+
text_to_summarize = st.text_area("Enter text to summarize:")
|
| 408 |
+
if st.button("Summarize"):
|
| 409 |
+
summary = summarize_text(text_to_summarize)
|
| 410 |
+
st.write(f"Summary: {summary}")
|
| 411 |
+
|
| 412 |
+
# Sentiment Analysis Tool
|
| 413 |
+
st.subheader("Sentiment Analysis")
|
| 414 |
+
sentiment_text = st.text_area("Enter text for sentiment analysis:")
|
| 415 |
+
if st.button("Analyze Sentiment"):
|
| 416 |
+
sentiment = sentiment_analysis(sentiment_text)
|
| 417 |
+
st.write(f"Sentiment: {sentiment}")
|
| 418 |
+
|
| 419 |
+
# Text Translation Tool (Code Translation)
|
| 420 |
+
st.subheader("Translate Code")
|
| 421 |
+
code_to_translate = st.text_area("Enter code to translate:")
|
| 422 |
+
source_language = st.text_input("Enter source language (e.g., 'Python'):")
|
| 423 |
+
target_language = st.text_input("Enter target language (e.g., 'JavaScript'):")
|
| 424 |
+
if st.button("Translate Code"):
|
| 425 |
+
translated_code = translate_code(code_to_translate, source_language, target_language)
|
| 426 |
+
st.code(translated_code, language=target_language.lower())
|
| 427 |
+
|
| 428 |
+
# Code Generation
|
| 429 |
+
st.subheader("Code Generation")
|
| 430 |
+
code_idea = st.text_input("Enter your code idea:")
|
| 431 |
+
if st.button("Generate Code"):
|
| 432 |
+
generated_code = generate_code(code_idea)
|
| 433 |
+
st.code(generated_code, language="python")
|
| 434 |
+
|
| 435 |
+
elif app_mode == "Workspace Chat App":
|
| 436 |
+
# Workspace Chat App
|
| 437 |
+
st.header("Workspace Chat App")
|
| 438 |
+
|
| 439 |
+
# Project Workspace Creation
|
| 440 |
+
st.subheader("Create a New Project")
|
| 441 |
+
project_name = st.text_input("Enter project name:")
|
| 442 |
+
if st.button("Create Project"):
|
| 443 |
+
workspace_status = workspace_interface(project_name)
|
| 444 |
+
st.success(workspace_status)
|
| 445 |
+
|
| 446 |
+
# Add Code to Workspace
|
| 447 |
+
st.subheader("Add Code to Workspace")
|
| 448 |
+
code_to_add = st.text_area("Enter code to add to workspace:")
|
| 449 |
+
file_name = st.text_input("Enter file name (e.g., 'app.py'):")
|
| 450 |
+
if st.button("Add Code"):
|
| 451 |
+
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
| 452 |
+
st.success(add_code_status)
|
| 453 |
+
|
| 454 |
+
# Terminal Interface with Project Context
|
| 455 |
+
st.subheader("Terminal (Workspace Context)")
|
| 456 |
+
terminal_input = st.text_input("Enter a command within the workspace:")
|
| 457 |
+
if st.button("Run Command"):
|
| 458 |
+
terminal_output = terminal_interface(terminal_input, project_name)
|
| 459 |
+
st.code(terminal_output, language="bash")
|
| 460 |
+
|
| 461 |
+
# Chat Interface for Guidance
|
| 462 |
+
st.subheader("Chat with CodeCraft for Guidance")
|
| 463 |
+
chat_input = st.text_area("Enter your message for guidance:")
|
| 464 |
+
if st.button("Get Guidance"):
|
| 465 |
+
chat_response = chat_interface(chat_input)
|
| 466 |
+
st.session_state.chat_history.append((chat_input, chat_response))
|
| 467 |
+
st.write(f"CodeCraft: {chat_response}")
|
| 468 |
+
|
| 469 |
+
# Display Chat History
|
| 470 |
+
st.subheader("Chat History")
|
| 471 |
+
for user_input, response in st.session_state.chat_history:
|
| 472 |
+
st.write(f"User: {user_input}")
|
| 473 |
+
st.write(f"CodeCraft: {response}")
|
| 474 |
+
|
| 475 |
+
# Display Terminal History
|
| 476 |
+
st.subheader("Terminal History")
|
| 477 |
+
for command, output in st.session_state.terminal_history:
|
| 478 |
+
st.write(f"Command: {command}")
|
| 479 |
+
st.code(output, language="bash")
|
| 480 |
+
|
| 481 |
+
# Display Projects and Files
|
| 482 |
+
st.subheader("Workspace Projects")
|
| 483 |
+
for project, details in st.session_state.workspace_projects.items():
|
| 484 |
+
st.write(f"Project: {project}")
|
| 485 |
+
for file in details['files']:
|
| 486 |
+
st.write(f" - {file}")
|