|
""" Simple Chatbot |
|
@author: Nigel Gebodh |
|
@email: [email protected] |
|
|
|
""" |
|
import numpy as np |
|
import streamlit as st |
|
from openai import OpenAI |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
|
|
client = OpenAI( |
|
base_url="https://api-inference.huggingface.co/v1", |
|
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') |
|
) |
|
|
|
|
|
model_links = { |
|
"Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct" |
|
} |
|
|
|
|
|
def reset_conversation(): |
|
st.session_state.conversation = [] |
|
st.session_state.messages = [] |
|
return None |
|
|
|
|
|
selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys())) |
|
|
|
|
|
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5) |
|
|
|
|
|
st.sidebar.button('Reset Chat', on_click=reset_conversation) |
|
|
|
|
|
st.sidebar.write(f"You're now chatting with **{selected_model}**") |
|
st.sidebar.markdown("*Generated content may be inaccurate or false.*") |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"]) |
|
|
|
if task_choice == "Data Generation": |
|
classification_type = st.selectbox( |
|
"Choose Classification Type", |
|
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"] |
|
) |
|
|
|
if classification_type == "Sentiment Analysis": |
|
st.write("Sentiment Analysis: Positive, Negative, Neutral") |
|
labels = ["Positive", "Negative", "Neutral"] |
|
elif classification_type == "Binary Classification": |
|
label_1 = st.text_input("Enter first class") |
|
label_2 = st.text_input("Enter second class") |
|
labels = [label_1, label_2] |
|
elif classification_type == "Multi-Class Classification": |
|
num_classes = st.slider("How many classes?", 3, 10, 3) |
|
labels = [st.text_input(f"Class {i+1}") for i in range(num_classes)] |
|
|
|
domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"]) |
|
if domain == "Custom": |
|
domain = st.text_input("Specify custom domain") |
|
|
|
min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10) |
|
max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90) |
|
|
|
few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"]) |
|
if few_shot == "Yes": |
|
num_examples = st.slider("How many few-shot examples?", 1, 5, 1) |
|
few_shot_examples = [ |
|
{"content": st.text_area(f"Example {i+1}"), "label": st.selectbox(f"Label for example {i+1}", labels)} |
|
for i in range(num_examples) |
|
] |
|
else: |
|
few_shot_examples = [] |
|
|
|
|
|
num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=50, value=10) |
|
|
|
|
|
system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate {num_to_generate} data examples for {domain}. " |
|
system_prompt += f"Each example should have a label and consist of between {min_words} and {max_words} words. " |
|
system_prompt += "Use the following labels: " + ", ".join(labels) + ". " |
|
|
|
if few_shot_examples: |
|
system_prompt += "Use the following few-shot examples as a reference:\n" |
|
for example in few_shot_examples: |
|
system_prompt += f"Example: {example['content']}, Label: {example['label']}\n" |
|
|
|
system_prompt += "Please only provide the examples in the following format:\n" |
|
system_prompt += "Example: <text>, Label: <label>\n" |
|
|
|
st.write("System Prompt:") |
|
st.code(system_prompt) |
|
|
|
if st.button("Generate Examples"): |
|
all_generated_examples = [] |
|
remaining_examples = num_to_generate |
|
|
|
with st.spinner("Generating..."): |
|
while remaining_examples > 0: |
|
chunk_size = min(remaining_examples, 5) |
|
try: |
|
st.session_state.messages.append({"role": "system", "content": system_prompt}) |
|
|
|
stream = client.chat.completions.create( |
|
model=model_links[selected_model], |
|
messages=[ |
|
{"role": m["role"], "content": m["content"]} |
|
for m in st.session_state.messages |
|
], |
|
temperature=temp_values, |
|
stream=True, |
|
max_tokens=3000, |
|
) |
|
|
|
response = st.write_stream(stream) |
|
|
|
|
|
generated_examples = response.split("Example: ")[1:chunk_size+1] |
|
|
|
|
|
all_generated_examples.extend(generated_examples) |
|
remaining_examples -= chunk_size |
|
|
|
except Exception as e: |
|
st.error("Error during generation.") |
|
st.write(e) |
|
break |
|
|
|
|
|
for idx, example in enumerate(all_generated_examples): |
|
st.write(f"Example {idx+1}: {example.strip()}") |
|
|
|
|
|
st.session_state.messages = [] |
|
|
|
else: |
|
|
|
st.write("Data Labeling functionality will go here.") |
|
|
|
|
|
|
|
|