Spaces:
Sleeping
Sleeping
acecalisto3
commited on
Commit
•
643e2e2
1
Parent(s):
8bcaecb
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,24 @@ import base64
|
|
5 |
import json
|
6 |
from io import StringIO
|
7 |
from typing import Dict, List
|
8 |
-
|
9 |
import streamlit as st
|
10 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
11 |
from pylint import lint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Replace st.secrets with os.environ
|
14 |
hf_token = os.environ.get("huggingface_token")
|
@@ -17,11 +31,6 @@ if not hf_token:
|
|
17 |
st.error("Hugging Face API key not found. Please set the HUGGINGFACE_API_KEY environment variable.")
|
18 |
st.stop()
|
19 |
|
20 |
-
# Rest of your code here
|
21 |
-
st.write("Hugging Face API key successfully loaded!")
|
22 |
-
|
23 |
-
# Rest of your code here
|
24 |
-
st.write("Hugging Face API key successfully loaded!")
|
25 |
# Global state to manage communication between Tool Box and Workspace Chat App
|
26 |
if "chat_history" not in st.session_state:
|
27 |
st.session_state.chat_history = []
|
@@ -31,13 +40,13 @@ if "workspace_projects" not in st.session_state:
|
|
31 |
st.session_state.workspace_projects = {}
|
32 |
|
33 |
# Load pre-trained RAG retriever
|
34 |
-
rag_retriever = pipeline("text-generation", model="
|
35 |
|
36 |
# Load pre-trained chat model
|
37 |
-
chat_model = AutoModelForSeq2SeqLM.from_pretrained("
|
38 |
|
39 |
# Load tokenizer
|
40 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
41 |
|
42 |
def process_input(user_input: str) -> str:
|
43 |
# Input pipeline: Tokenize and preprocess user input
|
@@ -90,7 +99,6 @@ class AIAgent:
|
|
90 |
# - Check if the user has requested to generate code
|
91 |
# - Check if the user has requested to translate code
|
92 |
# - Check if the user has requested to summarize text
|
93 |
-
# - Check if the user has requested to analyze sentiment
|
94 |
|
95 |
# Generate a response based on the analysis
|
96 |
next_step = "Based on the current state, the next logical step is to implement the main application logic."
|
@@ -170,7 +178,7 @@ def chat_interface_with_agent(input_text: str, agent_name: str) -> str:
|
|
170 |
if agent_prompt is None:
|
171 |
return f"Agent {agent_name} not found."
|
172 |
|
173 |
-
model_name = "
|
174 |
try:
|
175 |
generator = pipeline("text-generation", model=model_name)
|
176 |
generator.tokenizer.pad_token = generator.tokenizer.eos_token
|
@@ -214,16 +222,12 @@ def summarize_text(text: str) -> str:
|
|
214 |
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
215 |
return summary[0]['summary_text']
|
216 |
|
217 |
-
def sentiment_analysis(text: str) -> str:
|
218 |
-
analyzer = pipeline("sentiment-analysis")
|
219 |
-
result = analyzer(text)
|
220 |
-
return result[0]['label']
|
221 |
|
222 |
def translate_code(code: str, source_language: str, target_language: str) -> str:
|
223 |
# Use a Hugging Face translation model instead of OpenAI
|
224 |
# Example: English to Spanish
|
225 |
translator = pipeline(
|
226 |
-
"translation", model="
|
227 |
translated_code = translator(code, target_lang=target_language)[0]['translation_text']
|
228 |
return translated_code
|
229 |
|
@@ -239,7 +243,7 @@ def generate_code(code_idea: str, model_name: str) -> str:
|
|
239 |
def chat_interface(input_text: str) -> str:
|
240 |
"""Handles general chat interactions with the user."""
|
241 |
# Use a Hugging Face chatbot model or your own logic
|
242 |
-
chatbot = pipeline("text-generation", model="
|
243 |
response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
244 |
return response
|
245 |
|
@@ -344,13 +348,6 @@ elif app_mode == "Tool Box":
|
|
344 |
summary = summarize_text(text_to_summarize)
|
345 |
st.write(f"Summary: {summary}")
|
346 |
|
347 |
-
# Sentiment Analysis Tool
|
348 |
-
st.subheader("Sentiment Analysis")
|
349 |
-
sentiment_text = st.text_area("Enter text for sentiment analysis:")
|
350 |
-
if st.button("Analyze Sentiment"):
|
351 |
-
sentiment = sentiment_analysis(sentiment_text)
|
352 |
-
st.write(f"Sentiment: {sentiment}")
|
353 |
-
|
354 |
# Text Translation Tool (Code Translation)
|
355 |
st.subheader("Translate Code")
|
356 |
code_to_translate = st.text_area("Enter code to translate:")
|
|
|
5 |
import json
|
6 |
from io import StringIO
|
7 |
from typing import Dict, List
|
|
|
8 |
import streamlit as st
|
9 |
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
10 |
from pylint import lint
|
11 |
+
from huggingface_hub import InferenceClient
|
12 |
+
import gradio as gr
|
13 |
+
import random
|
14 |
+
import prompts
|
15 |
+
client = InferenceClient(
|
16 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
17 |
+
)
|
18 |
+
|
19 |
+
def format_prompt(message, history):
|
20 |
+
prompt = "<s>"
|
21 |
+
for user_prompt, bot_response in history:
|
22 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
23 |
+
prompt += f" {bot_response}</s> "
|
24 |
+
prompt += f"[INST] {message} [/INST]"
|
25 |
+
return prompt
|
26 |
|
27 |
# Replace st.secrets with os.environ
|
28 |
hf_token = os.environ.get("huggingface_token")
|
|
|
31 |
st.error("Hugging Face API key not found. Please set the HUGGINGFACE_API_KEY environment variable.")
|
32 |
st.stop()
|
33 |
|
|
|
|
|
|
|
|
|
|
|
34 |
# Global state to manage communication between Tool Box and Workspace Chat App
|
35 |
if "chat_history" not in st.session_state:
|
36 |
st.session_state.chat_history = []
|
|
|
40 |
st.session_state.workspace_projects = {}
|
41 |
|
42 |
# Load pre-trained RAG retriever
|
43 |
+
rag_retriever = pipeline("text-generation", model="mistralai/Mixtral-8x7B-v0.1")
|
44 |
|
45 |
# Load pre-trained chat model
|
46 |
+
chat_model = AutoModelForSeq2SeqLM.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
|
47 |
|
48 |
# Load tokenizer
|
49 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
|
50 |
|
51 |
def process_input(user_input: str) -> str:
|
52 |
# Input pipeline: Tokenize and preprocess user input
|
|
|
99 |
# - Check if the user has requested to generate code
|
100 |
# - Check if the user has requested to translate code
|
101 |
# - Check if the user has requested to summarize text
|
|
|
102 |
|
103 |
# Generate a response based on the analysis
|
104 |
next_step = "Based on the current state, the next logical step is to implement the main application logic."
|
|
|
178 |
if agent_prompt is None:
|
179 |
return f"Agent {agent_name} not found."
|
180 |
|
181 |
+
model_name = ""
|
182 |
try:
|
183 |
generator = pipeline("text-generation", model=model_name)
|
184 |
generator.tokenizer.pad_token = generator.tokenizer.eos_token
|
|
|
222 |
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
223 |
return summary[0]['summary_text']
|
224 |
|
|
|
|
|
|
|
|
|
225 |
|
226 |
def translate_code(code: str, source_language: str, target_language: str) -> str:
|
227 |
# Use a Hugging Face translation model instead of OpenAI
|
228 |
# Example: English to Spanish
|
229 |
translator = pipeline(
|
230 |
+
"translation", model="mistralai/Mixtral-8x7B-Instruct-v0.1")
|
231 |
translated_code = translator(code, target_lang=target_language)[0]['translation_text']
|
232 |
return translated_code
|
233 |
|
|
|
243 |
def chat_interface(input_text: str) -> str:
|
244 |
"""Handles general chat interactions with the user."""
|
245 |
# Use a Hugging Face chatbot model or your own logic
|
246 |
+
chatbot = pipeline("text-generation", model="mistralai/Mixtral-8x7B-Instruct-v0.1")
|
247 |
response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
|
248 |
return response
|
249 |
|
|
|
348 |
summary = summarize_text(text_to_summarize)
|
349 |
st.write(f"Summary: {summary}")
|
350 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
351 |
# Text Translation Tool (Code Translation)
|
352 |
st.subheader("Translate Code")
|
353 |
code_to_translate = st.text_area("Enter code to translate:")
|