Wedyan2023 commited on
Commit
6cdc48c
·
verified ·
1 Parent(s): 21bafe7

Create app4.py

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