Wedyan2023 commited on
Commit
dea01ac
·
verified ·
1 Parent(s): f21a672

Delete app4.py

Browse files
Files changed (1) hide show
  1. app4.py +0 -153
app4.py DELETED
@@ -1,153 +0,0 @@
1
- #""" Simple Chatbot
2
- #@author: Nigel Gebodh
3
- #@email: nigel.gebodh@gmail.com
4
- #"""
5
- """ Simple Chatbot
6
- @author: Wedyan2023
7
- @email: w.s.alskaran2@gmail.com
8
- """
9
- import numpy as np
10
- import streamlit as st
11
- from openai import OpenAI
12
- import os
13
- from dotenv import load_dotenv
14
- import random
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
-
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
- # User prompt text field
109
- user_prompt = st.text_area("Enter your prompt to guide example generation", "")
110
-
111
- # System prompt generation
112
- system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
113
- if few_shot_examples:
114
- system_prompt += "Use the following few-shot examples as a reference:\n"
115
- for example in few_shot_examples:
116
- system_prompt += f"Example: {example['content']} \n Label: {example['label']}\n"
117
- system_prompt += f"Generate {num_to_generate} unique examples with diverse phrasing.\n"
118
- system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
119
- system_prompt += f"Use the labels specified: {', '.join(labels)}.\n"
120
- if user_prompt:
121
- system_prompt += f"Additional instructions: {user_prompt}\n"
122
-
123
- st.write("System Prompt:")
124
- st.code(system_prompt)
125
-
126
- if st.button("Generate Examples"):
127
- # Generate examples by concatenating all inputs and sending it to the model
128
- with st.spinner("Generating..."):
129
- st.session_state.messages.append({"role": "system", "content": system_prompt})
130
-
131
- try:
132
- stream = client.chat.completions.create(
133
- model=model_links[selected_model],
134
- messages=[
135
- {"role": m["role"], "content": m["content"]}
136
- for m in st.session_state.messages
137
- ],
138
- temperature=temp_values,
139
- stream=True,
140
- max_tokens=3000,
141
- )
142
- response = st.write_stream(stream)
143
- except Exception as e:
144
- response = "Error during generation."
145
- random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
146
- st.image(random_dog_pick)
147
- st.write(e)
148
-
149
- st.session_state.messages.append({"role": "assistant", "content": response})
150
-
151
- else:
152
- # Data labeling workflow (for future implementation based on classification)
153
- st.write("Data Labeling functionality will go here.")