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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="NeuraNET API Playground", page_icon="https://neuranet-ai.com/static/img/cover.png")
|
| 6 |
+
|
| 7 |
+
st.sidebar.markdown("<h1 style='text-align: center;'>Settings</h1>", unsafe_allow_html=True)
|
| 8 |
+
|
| 9 |
+
model_type = st.sidebar.selectbox('Which Type of AI Model?', ('Chat', 'Image', 'TTS'))
|
| 10 |
+
|
| 11 |
+
st.markdown("<p style='text-align: center;'><img src='https://neuranet-ai.com/static/img/cover.png' style='width: 20%; height: auto;'></p>", unsafe_allow_html=True)
|
| 12 |
+
st.markdown("<h1 style='text-align: center;'>NeuraNET API Playground</h1>", unsafe_allow_html=True)
|
| 13 |
+
|
| 14 |
+
NEURANET_API_KEY = st.sidebar.text_input('Enter your NeuraNET API Key', type='password', autocomplete='off')
|
| 15 |
+
if not NEURANET_API_KEY:
|
| 16 |
+
st.error('Please enter your NeuraNET API Key.')
|
| 17 |
+
st.stop()
|
| 18 |
+
|
| 19 |
+
headers = {
|
| 20 |
+
'Authorization': f'Bearer {NEURANET_API_KEY}',
|
| 21 |
+
'Content-Type': 'application/json'
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
if model_type == 'Image':
|
| 25 |
+
st.markdown("<p style='text-align: center;'>The NeuraNET Text-To-Image (Diffusion) Models, referred to as Vinci.</p>", unsafe_allow_html=True)
|
| 26 |
+
|
| 27 |
+
model_alias = st.sidebar.selectbox('Choose a model', ('Vinci Mini', 'Vinci Max'))
|
| 28 |
+
model = 'vinci-mini' if model_alias == 'Vinci Mini' else 'vinci-max'
|
| 29 |
+
|
| 30 |
+
dimensions = st.sidebar.selectbox('Image dimensions', ('square', 'portrait', 'landscape'))
|
| 31 |
+
|
| 32 |
+
user_input = st.text_input("Type your prompt here")
|
| 33 |
+
|
| 34 |
+
if st.button('Generate Image'):
|
| 35 |
+
if not user_input:
|
| 36 |
+
st.error("Prompt is empty. Please type your message.")
|
| 37 |
+
st.stop()
|
| 38 |
+
|
| 39 |
+
data = {
|
| 40 |
+
'content': {
|
| 41 |
+
'model': model,
|
| 42 |
+
'prompt': user_input,
|
| 43 |
+
'size': dimensions
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
response = requests.post('https://neuranet-ai.com/api/v1/image', headers=headers, data=json.dumps(data))
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
response_data = response.json()
|
| 51 |
+
|
| 52 |
+
if 'result' in response_data and len(response_data['result']) > 0:
|
| 53 |
+
image_url = response_data['result'][0]['result-url']
|
| 54 |
+
st.image(image_url, caption='Generated Image', use_column_width=True)
|
| 55 |
+
else:
|
| 56 |
+
st.error('An error occurred, possibly due to an invalid API key, server issues, or an overly long message.')
|
| 57 |
+
st.stop()
|
| 58 |
+
|
| 59 |
+
except ValueError:
|
| 60 |
+
st.error('Decoding JSON has failed')
|
| 61 |
+
st.stop()
|
| 62 |
+
|
| 63 |
+
elif model_type == 'Chat':
|
| 64 |
+
st.markdown("<p style='text-align: center;'>NeuraNET Text Generation (Chat) Models - Chat History is not supported here.</p>", unsafe_allow_html=True)
|
| 65 |
+
|
| 66 |
+
model_alias = st.sidebar.selectbox('Choose a model', ('NeuraNET Lite', 'NeuraNET Hyper', 'NeuraNET Hyper Web'))
|
| 67 |
+
model = 'nlite' if model_alias == 'NeuraNET Lite' else 'neuranet-hyper-5x185b' if model_alias == 'NeuraNET Hyper' else 'neuranet-hyper-web-5x185b'
|
| 68 |
+
|
| 69 |
+
instruct_input = st.sidebar.text_area("Instruct Prompt (Optional)", height=300)
|
| 70 |
+
|
| 71 |
+
history = []
|
| 72 |
+
if instruct_input:
|
| 73 |
+
history.append({
|
| 74 |
+
"sender": "instruct",
|
| 75 |
+
"content": instruct_input
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
user_input = st.text_input("Type your message")
|
| 79 |
+
|
| 80 |
+
if st.button('Send Message'):
|
| 81 |
+
if not user_input:
|
| 82 |
+
st.error("User input is empty. Please type your message.")
|
| 83 |
+
st.stop()
|
| 84 |
+
|
| 85 |
+
user_message = {
|
| 86 |
+
"sender": "user",
|
| 87 |
+
"content": user_input
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
if model == 'nweb':
|
| 91 |
+
try:
|
| 92 |
+
search_response = requests.get(f'https://search.neuranet-ai.com/search?query={user_input}&limit=5')
|
| 93 |
+
search_results = search_response.json()
|
| 94 |
+
|
| 95 |
+
if search_results:
|
| 96 |
+
web_instruct_message = "You are a special version of the NeuraNET Hyper model called 'NeuraNET Hyper Web', you have the ability to indirectly search the internet. You will now receive the search results of what the user said. It will be formatted like this: 'Title - Source Link - Snippet'.You will receive user queries and present the corresponding search results in this format. When creating a response to the user, do not give the user the raw search results, tell it to them in a friendly, informative, and casual way. Here's the user's query: \n\n"
|
| 97 |
+
for result in search_results:
|
| 98 |
+
web_instruct_message += f"{result['title']} - {result['link']} - {result['snippet']}\n"
|
| 99 |
+
else:
|
| 100 |
+
web_instruct_message = "Request failed or is empty."
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
web_instruct_message = "Request failed or is empty."
|
| 104 |
+
|
| 105 |
+
history.append({
|
| 106 |
+
"sender": "instruct",
|
| 107 |
+
"content": web_instruct_message
|
| 108 |
+
})
|
| 109 |
+
|
| 110 |
+
history.append(user_message)
|
| 111 |
+
|
| 112 |
+
data = {
|
| 113 |
+
'settings': {
|
| 114 |
+
'model': 'neuranet-hyper-5x185b' if model == 'neuranet-hyper-web-5x185b' else model
|
| 115 |
+
},
|
| 116 |
+
'conversation': {
|
| 117 |
+
'history': history
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
response = requests.post('https://neuranet-ai.com/api/v1/chat', headers=headers, data=json.dumps(data))
|
| 122 |
+
|
| 123 |
+
try:
|
| 124 |
+
response_data = response.json()
|
| 125 |
+
ai_response = response_data['choices'][0]['text']
|
| 126 |
+
st.write(ai_response)
|
| 127 |
+
except KeyError:
|
| 128 |
+
st.error(f'An error occurred, possibly due to an invalid API key, server issues, or an overly long message.')
|
| 129 |
+
st.stop()
|
| 130 |
+
|
| 131 |
+
elif model_type == 'TTS':
|
| 132 |
+
st.markdown("<p style='text-align: center;'>The NeuraNET Text-To-Speech (TTS) Model, referred to as NeuraNET Mint.</p>", unsafe_allow_html=True)
|
| 133 |
+
|
| 134 |
+
voices_response = requests.get('https://neuranet-ai.com/api/v1/tts/voices')
|
| 135 |
+
voices_data = voices_response.json()
|
| 136 |
+
|
| 137 |
+
types = set()
|
| 138 |
+
voices_by_type = {}
|
| 139 |
+
for voice_entry in voices_data:
|
| 140 |
+
voice_type = voice_entry['type']
|
| 141 |
+
voice_name = voice_entry['voice']
|
| 142 |
+
types.add(voice_type)
|
| 143 |
+
if voice_type in voices_by_type:
|
| 144 |
+
voices_by_type[voice_type].append(voice_name)
|
| 145 |
+
else:
|
| 146 |
+
voices_by_type[voice_type] = [voice_name]
|
| 147 |
+
|
| 148 |
+
types.discard('en-US')
|
| 149 |
+
selected_type = st.sidebar.selectbox('Select Type', ['en-US'] + sorted(types), index=0)
|
| 150 |
+
available_voices = voices_by_type[selected_type] if selected_type in voices_by_type else []
|
| 151 |
+
selected_voice = st.sidebar.selectbox('Select Voice', available_voices, index=available_voices.index('Eric') if 'Eric' in available_voices else 0)
|
| 152 |
+
|
| 153 |
+
user_input = st.text_area("Enter the text for TTS")
|
| 154 |
+
|
| 155 |
+
if st.button('Generate Speech'):
|
| 156 |
+
if not user_input:
|
| 157 |
+
st.error("Text is empty. Please type your message.")
|
| 158 |
+
st.stop()
|
| 159 |
+
if not selected_type:
|
| 160 |
+
st.error("Please select a type.")
|
| 161 |
+
st.stop()
|
| 162 |
+
|
| 163 |
+
data = {
|
| 164 |
+
'settings': {
|
| 165 |
+
'type': selected_type if selected_type else 'en-US',
|
| 166 |
+
'voice': selected_voice
|
| 167 |
+
},
|
| 168 |
+
'say': user_input
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
response = requests.post('https://neuranet-ai.com/api/v1/tts', headers=headers, data=json.dumps(data))
|
| 172 |
+
|
| 173 |
+
try:
|
| 174 |
+
response_data = response.json()
|
| 175 |
+
audio_url = response_data['result'][0]['result-url']
|
| 176 |
+
st.audio(audio_url, format='audio/mp3', start_time=0)
|
| 177 |
+
except KeyError:
|
| 178 |
+
st.error('An error occurred, possibly due to an invalid API key, server issues, or an overly long message.')
|
| 179 |
+
st.stop()
|