Spaces:
Sleeping
Sleeping
Create backupapp.py
Browse files- backupapp.py +58 -0
backupapp.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
from streamlit_chat import message
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from openai import ChatCompletion
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
openai.api_key = os.getenv('OPENAI_KEY')
|
12 |
+
# keys are here: https://platform.openai.com/auth/callback?code=ReZ4izEw0DwkUKrHR-Opxr5AMMgo9SojxC9pNHQUcjD6M&state=OGZFNDJmLlJGNlIwOUxlakpXZkVFfjNxNy02ZlFtLWN4eUcuOXJobXouSQ%3D%3D#
|
13 |
+
|
14 |
+
# Define a function to chat with the model
|
15 |
+
def chat_with_model(prompts):
|
16 |
+
model = "gpt-3.5-turbo" # change this to the model you're using
|
17 |
+
|
18 |
+
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
19 |
+
conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
|
20 |
+
|
21 |
+
response = openai.ChatCompletion.create(model=model, messages=conversation)
|
22 |
+
return response['choices'][0]['message']['content']
|
23 |
+
|
24 |
+
load_dotenv('api_key.env')
|
25 |
+
|
26 |
+
def generate_response(prompt):
|
27 |
+
completion=openai.Completion.create(
|
28 |
+
engine='text-davinci-003',
|
29 |
+
prompt=prompt,
|
30 |
+
max_tokens=1024,
|
31 |
+
n=1,
|
32 |
+
stop=None,
|
33 |
+
temperature=0.6,
|
34 |
+
)
|
35 |
+
message=completion.choices[0].text
|
36 |
+
return message
|
37 |
+
|
38 |
+
# Streamlit App
|
39 |
+
def main():
|
40 |
+
st.title("Chat with AI")
|
41 |
+
|
42 |
+
# Pre-defined prompts
|
43 |
+
prompts = ['Hows the weather?', 'Tell me a joke.', 'What is the meaning of life?']
|
44 |
+
|
45 |
+
# User prompt input
|
46 |
+
user_prompt = st.text_input("Your question:", '')
|
47 |
+
|
48 |
+
if user_prompt:
|
49 |
+
prompts.append(user_prompt)
|
50 |
+
|
51 |
+
if st.button('Chat'):
|
52 |
+
st.write('Chatting with GPT-3...')
|
53 |
+
response = chat_with_model(prompts)
|
54 |
+
st.write('Response:')
|
55 |
+
st.write(response)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
main()
|