Kaludi commited on
Commit
dc98d0d
1 Parent(s): ce1ac7e

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +5 -4
  2. app.py +65 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: OpenAI-Chatbot App
3
- emoji: 🔥
4
- colorFrom: indigo
5
- colorTo: yellow
6
  sdk: streamlit
7
  sdk_version: 1.17.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: OpenAI Chatbot App
3
+ emoji: 🤖
4
+ colorFrom: purple
5
+ colorTo: blue
6
  sdk: streamlit
7
  sdk_version: 1.17.0
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+
5
+ st.title("OpenAI Chatbot")
6
+ st.write("Interact with OpenAI's models in real-time using your OpenAI API. Choose from a selection of their best models, set the temperature and max tokens, and start a conversation. Delete the conversation at any time and start fresh.")
7
+
8
+ if "history" not in st.session_state:
9
+ st.session_state.history = []
10
+
11
+ st.sidebar.markdown("## Configuration")
12
+ API_KEY = st.sidebar.text_input("Enter your OpenAI API key")
13
+ models = ['text-davinci-003', 'text-curie-001', 'text-babbage-001', 'text-ada-001']
14
+ model = st.sidebar.selectbox("Select a model", models, index=0)
15
+
16
+ temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.7)
17
+ max_tokens = st.sidebar.slider("Max Tokens", 0, 3024, 3024)
18
+
19
+ if st.sidebar.button("Delete Conversation"):
20
+ st.session_state.history = []
21
+
22
+ st.sidebar.markdown("---")
23
+ st.sidebar.markdown("## GPT-3")
24
+ st.sidebar.markdown("OpenAI's GPT-3 models can understand and generate natural language. They offer four main models with different levels of power suitable for different tasks. Davinci is the most capable model, and Ada is the fastest.")
25
+
26
+ st.sidebar.markdown("text-davinci-003 | 4,000 max tokens")
27
+ st.sidebar.markdown("text-curie-001 | 2,048 max tokens")
28
+ st.sidebar.markdown("text-babbage-001 | 2,048 max tokens")
29
+ st.sidebar.markdown("text-ada-001 | 2,048 max tokens")
30
+
31
+ def generate_answer(prompt):
32
+ API_KEY = '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'
33
+ API_URL = "https://api.openai.com/v1/completions"
34
+ headers = {
35
+ 'Content-Type': 'application/json',
36
+ 'Authorization': 'Bearer ' + API_KEY
37
+ }
38
+ previous_messages = [chat['message'] for chat in st.session_state.history if not chat['is_user']]
39
+ previous_messages_text = '\n'.join(previous_messages)
40
+ full_prompt = previous_messages_text + '\n' + prompt if previous_messages_text else prompt
41
+ data = {
42
+ "model": "text-davinci-003",
43
+ "prompt": full_prompt,
44
+ "temperature": 0.7,
45
+ "max_tokens": 3024
46
+ }
47
+ response = requests.post(API_URL, headers=headers, data=json.dumps(data))
48
+ result = response.json()
49
+ message_bot = result['choices'][0]['text']
50
+ st.session_state.history.append({"message": prompt, "is_user": True})
51
+ st.session_state.history.append({"message": message_bot, "is_user": False})
52
+
53
+
54
+
55
+ prompt = st.text_input("Prompt")
56
+ if st.button("Submit"):
57
+ generate_answer(prompt)
58
+ with st.spinner("Waiting for the response from the bot..."):
59
+ for chat in st.session_state.history:
60
+ if chat['is_user']:
61
+ st.markdown("<img src='https://i.ibb.co/zVSbGvb/585e4beacb11b227491c3399.png' width='50' height='50' style='float:right;'>", unsafe_allow_html=True)
62
+ st.markdown(f"<div style='float:right; padding:10px; background-color: #f2f2f2; border-radius:10px; margin:10px;'>{chat['message']}</div>", unsafe_allow_html=True)
63
+ else:
64
+ st.markdown("<img src='https://i.ibb.co/LZFvDND/5841c0bda6515b1e0ad75a9e-1.png' width='50' height='50' style='float:left;'>", unsafe_allow_html=True)
65
+ st.markdown(f"<div style='float:left; padding:10px; background-color: #f2f2f2; border-radius:10px; margin:10px;'>{chat['message']}</div>", unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastai==2.7.4
2
+ huggingface_hub[fastai]
3
+ fastcore>=1.3.27