Delete app.py
Browse files
app.py
DELETED
@@ -1,165 +0,0 @@
|
|
1 |
-
#""" Simple Chatbot
|
2 |
-
#@author: Nigel Gebodh
|
3 |
-
#@email: [email protected]
|
4 |
-
#"""
|
5 |
-
""" Simple Chatbot
|
6 |
-
@author: Wedyan2023
|
7 |
-
@email: [email protected]
|
8 |
-
"""
|
9 |
-
|
10 |
-
import numpy as np
|
11 |
-
import streamlit as st
|
12 |
-
from openai import OpenAI
|
13 |
-
import os
|
14 |
-
from dotenv import load_dotenv
|
15 |
-
import random
|
16 |
-
os.environ["BROWSER_GATHERUSAGESTATS"] = "false"
|
17 |
-
|
18 |
-
load_dotenv()
|
19 |
-
## Embedding Using Huggingface
|
20 |
-
#huggingface_embeddings=HuggingFaceBgeEmbeddings(
|
21 |
-
#model_name="BAAI/bge-small-en-v1.5", #sentence-transformers/all-MiniLM-l6-v2
|
22 |
-
#model_kwargs={'device':'cpu'},
|
23 |
-
#encode_kwargs={'normalize_embeddings':True}
|
24 |
-
|
25 |
-
#)
|
26 |
-
|
27 |
-
# Initialize the client
|
28 |
-
client = OpenAI(
|
29 |
-
base_url="https://api-inference.huggingface.co/v1",
|
30 |
-
#api_key=os.environ.get('HUGGINGFACE_API_TOKEN') # Add your Huggingface token here
|
31 |
-
api_key=os.environ.get('TOKEN2') # Add your Huggingface token here
|
32 |
-
)
|
33 |
-
|
34 |
-
# Supported models
|
35 |
-
model_links = {
|
36 |
-
"Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct"
|
37 |
-
}
|
38 |
-
|
39 |
-
# Random dog images for error messages
|
40 |
-
#random_dog = [
|
41 |
-
#"0f476473-2d8b-415e-b944-483768418a95.jpg",
|
42 |
-
#"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
43 |
-
#"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
44 |
-
# "1326984c-39b0-492c-a773-f120d747a7e2.jpg"
|
45 |
-
#]
|
46 |
-
|
47 |
-
# Reset conversation
|
48 |
-
def reset_conversation():
|
49 |
-
st.session_state.conversation = []
|
50 |
-
st.session_state.messages = []
|
51 |
-
return None
|
52 |
-
|
53 |
-
# Define the available models
|
54 |
-
models = [key for key in model_links.keys()]
|
55 |
-
|
56 |
-
# Sidebar for model selection
|
57 |
-
selected_model = st.sidebar.selectbox("Select Model", models)
|
58 |
-
|
59 |
-
# Temperature slider
|
60 |
-
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
|
61 |
-
|
62 |
-
# Reset button
|
63 |
-
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
64 |
-
|
65 |
-
# Model description
|
66 |
-
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
67 |
-
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
68 |
-
|
69 |
-
# Chat initialization
|
70 |
-
if "messages" not in st.session_state:
|
71 |
-
st.session_state.messages = []
|
72 |
-
|
73 |
-
# Display chat messages
|
74 |
-
for message in st.session_state.messages:
|
75 |
-
with st.chat_message(message["role"]):
|
76 |
-
st.markdown(message["content"])
|
77 |
-
|
78 |
-
# Main logic to choose between data generation and data labeling
|
79 |
-
task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"])
|
80 |
-
|
81 |
-
if task_choice == "Data Generation":
|
82 |
-
classification_type = st.selectbox(
|
83 |
-
"Choose Classification Type",
|
84 |
-
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
|
85 |
-
)
|
86 |
-
|
87 |
-
if classification_type == "Sentiment Analysis":
|
88 |
-
st.write("Sentiment Analysis: Positive, Negative, Neutral")
|
89 |
-
labels = ["Positive", "Negative", "Neutral"]
|
90 |
-
elif classification_type == "Binary Classification":
|
91 |
-
label_1 = st.text_input("Enter first class")
|
92 |
-
label_2 = st.text_input("Enter second class")
|
93 |
-
labels = [label_1, label_2]
|
94 |
-
elif classification_type == "Multi-Class Classification":
|
95 |
-
num_classes = st.slider("How many classes?", 3, 10, 3)
|
96 |
-
labels = [st.text_input(f"Class {i+1}") for i in range(num_classes)]
|
97 |
-
|
98 |
-
domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"])
|
99 |
-
if domain == "Custom":
|
100 |
-
domain = st.text_input("Specify custom domain")
|
101 |
-
|
102 |
-
min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10)
|
103 |
-
max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90)
|
104 |
-
|
105 |
-
few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"])
|
106 |
-
if few_shot == "Yes":
|
107 |
-
num_examples = st.slider("How many few-shot examples?", 1, 5, 1)
|
108 |
-
few_shot_examples = [
|
109 |
-
{"content": st.text_area(f"Example {i+1}"), "label": st.selectbox(f"Label for example {i+1}", labels)}
|
110 |
-
for i in range(num_examples)
|
111 |
-
]
|
112 |
-
else:
|
113 |
-
few_shot_examples = []
|
114 |
-
|
115 |
-
# Ask the user how many examples they need
|
116 |
-
num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=100, value=10)
|
117 |
-
|
118 |
-
# User prompt text field
|
119 |
-
user_prompt = st.text_area("Enter your prompt to guide example generation", "")
|
120 |
-
|
121 |
-
# System prompt generation
|
122 |
-
system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
|
123 |
-
if few_shot_examples:
|
124 |
-
system_prompt += "Use the following few-shot examples as a reference:\n"
|
125 |
-
for example in few_shot_examples:
|
126 |
-
system_prompt += f"Example: {example['content']} \n Label: {example['label']}\n"
|
127 |
-
system_prompt += f"Generate {num_to_generate} unique examples with diverse phrasing.\n"
|
128 |
-
system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
|
129 |
-
system_prompt += f"Use the labels specified: {', '.join(labels)}.\n"
|
130 |
-
if user_prompt:
|
131 |
-
system_prompt += f"Additional instructions: {user_prompt}\n"
|
132 |
-
|
133 |
-
st.write("System Prompt:")
|
134 |
-
st.code(system_prompt)
|
135 |
-
|
136 |
-
if st.button("Generate Examples"):
|
137 |
-
# Generate examples by concatenating all inputs and sending it to the model
|
138 |
-
with st.spinner("Generating..."):
|
139 |
-
st.session_state.messages.append({"role": "system", "content": system_prompt})
|
140 |
-
|
141 |
-
try:
|
142 |
-
stream = client.chat.completions.create(
|
143 |
-
model=model_links[selected_model],
|
144 |
-
messages=[
|
145 |
-
{"role": m["role"], "content": m["content"]}
|
146 |
-
for m in st.session_state.messages
|
147 |
-
],
|
148 |
-
temperature=temp_values,
|
149 |
-
stream=True,
|
150 |
-
max_tokens=3000,
|
151 |
-
)
|
152 |
-
response = st.write_stream(stream)
|
153 |
-
except Exception as e:
|
154 |
-
response = "Error during generation."
|
155 |
-
random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
|
156 |
-
st.image(random_dog_pick)
|
157 |
-
st.write(e)
|
158 |
-
|
159 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
160 |
-
|
161 |
-
else:
|
162 |
-
# Data labeling workflow (for future implementation based on classification)
|
163 |
-
st.write("Data Labeling functionality will go here.")
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|