File size: 1,447 Bytes
698a47e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
from dotenv import load_dotenv
import streamlit as st
import openai


# Function to chat with GPT
def chat_with_gpt(prompt):
    load_dotenv()
    openai.api_key = os.getenv("OPENAI_API_KEY")

    result = openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ]
    )
    return result.choices[0].message.content


# Streamlit UI
def main():
    st.title("Generation and Analysis Tool with OpenAI")

    # Add text input field
    prompt = st.text_input("Please enter a question or request:")

    # Interaction options
    max_tokens = st.sidebar.slider("Max Tokens", min_value=10, max_value=200, value=50, step=10,
                                   help="Maximum number of tokens to generate")
    temperature = st.sidebar.slider("Temperature", min_value=0.1, max_value=1.0, value=0.5, step=0.1,
                                    help="Controls the randomness of the generated text. Higher values make the text "
                                         "more random.")

    # Add a button to trigger the chat
    if st.button("Enter"):
        if prompt.strip() == '':
            st.warning("Please enter a valid prompt.")
        else:
            response = chat_with_gpt(prompt)
            st.success("Here's the response:")
            st.write(response)


if __name__ == "__main__":
    main()