Spaces:
Running
Running
Raman Singh01
commited on
Commit
•
9ab28ae
1
Parent(s):
f155579
Added file
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
6 |
+
|
7 |
+
def generate_response(
|
8 |
+
prompt,
|
9 |
+
temperature=0.7,
|
10 |
+
max_tokens=256,
|
11 |
+
top_p=0.9,
|
12 |
+
n=2,
|
13 |
+
stop=None,
|
14 |
+
frequency_penalty=0.9,
|
15 |
+
presence_penalty=0.9,
|
16 |
+
chat_history=None,
|
17 |
+
):
|
18 |
+
if chat_history is None:
|
19 |
+
chat_history = []
|
20 |
+
|
21 |
+
messages = [
|
22 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
23 |
+
{"role": "user", "content": prompt},
|
24 |
+
]
|
25 |
+
messages.extend(chat_history)
|
26 |
+
|
27 |
+
response = openai.ChatCompletion.create(
|
28 |
+
model="gpt-3.5-turbo",
|
29 |
+
messages=messages,
|
30 |
+
temperature=temperature,
|
31 |
+
max_tokens=max_tokens,
|
32 |
+
top_p=top_p,
|
33 |
+
n=n,
|
34 |
+
stop=stop,
|
35 |
+
frequency_penalty=frequency_penalty,
|
36 |
+
presence_penalty=presence_penalty,
|
37 |
+
)
|
38 |
+
|
39 |
+
return response["choices"][0]["message"]["content"]
|
40 |
+
|
41 |
+
|
42 |
+
logo1 = "https://www.ramanpre.com/_vercel/image?url=_astro%2Fheadshot.Bg7_IgN-.png&w=750&q=100"
|
43 |
+
|
44 |
+
st.set_page_config(
|
45 |
+
page_title="Health Care Personal Assistant | Data Admirers",
|
46 |
+
page_icon=logo1,
|
47 |
+
layout="wide",
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
st.write("# Personal Chatbot for all your health need")
|
52 |
+
|
53 |
+
st.sidebar.markdown("# Model Parameters")
|
54 |
+
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.7, 0.1)
|
55 |
+
max_tokens = st.sidebar.number_input("Max Tokens", 50, 500, 256, step=50)
|
56 |
+
top_p = st.sidebar.slider("Top P", 0.1, 1.0, 0.9, 0.1)
|
57 |
+
n = st.sidebar.number_input("N", 1, 5, 2, step=1)
|
58 |
+
stop = st.sidebar.text_input("Stop", "")
|
59 |
+
frequency_penalty = st.sidebar.slider("Frequency Penalty", 0.0, 1.0, 0.9, 0.1)
|
60 |
+
presence_penalty = st.sidebar.slider("Presence Penalty", 0.0, 1.0, 0.9, 0.1)
|
61 |
+
|
62 |
+
user_input = st.text_area("You:", "", key="user_input")
|
63 |
+
generate_button = st.button("Generate Response")
|
64 |
+
|
65 |
+
messages = []
|
66 |
+
if user_input.strip() != "":
|
67 |
+
messages.append({"role": "user", "content": user_input})
|
68 |
+
response = generate_response(
|
69 |
+
user_input,
|
70 |
+
temperature,
|
71 |
+
max_tokens,
|
72 |
+
top_p,
|
73 |
+
n,
|
74 |
+
stop,
|
75 |
+
frequency_penalty,
|
76 |
+
presence_penalty,
|
77 |
+
)
|
78 |
+
messages.append({"role": "assistant", "content": response})
|
79 |
+
|
80 |
+
st.subheader("Chat History")
|
81 |
+
for message in messages:
|
82 |
+
if message["role"] == "user":
|
83 |
+
st.text_area(
|
84 |
+
"You:",
|
85 |
+
value=message["content"],
|
86 |
+
height=50,
|
87 |
+
max_chars=200,
|
88 |
+
key="user_history",
|
89 |
+
disabled=True,
|
90 |
+
)
|
91 |
+
else:
|
92 |
+
st.text_area(
|
93 |
+
"Jarvis:", value=message["content"], height=500, key="chatbot_history"
|
94 |
+
)
|
95 |
+
|
96 |
+
st.markdown(
|
97 |
+
"""
|
98 |
+
<style>
|
99 |
+
body {
|
100 |
+
font-family: Montserrat, sans-serif;
|
101 |
+
}
|
102 |
+
.stTextInput>div>div>textarea {
|
103 |
+
background-color: #f0f0f0;
|
104 |
+
color: #000;
|
105 |
+
}
|
106 |
+
.stButton button {
|
107 |
+
background-color: #4CAF50;
|
108 |
+
color: white;
|
109 |
+
font-weight: bold;
|
110 |
+
}
|
111 |
+
.stTextArea>div>textarea {
|
112 |
+
resize: none;
|
113 |
+
}
|
114 |
+
.st-subheader {
|
115 |
+
margin-top: 20px;
|
116 |
+
font-size: 16px;
|
117 |
+
}
|
118 |
+
.stTextArea>div>div>textarea {
|
119 |
+
height: 100px;
|
120 |
+
}
|
121 |
+
</style>
|
122 |
+
""",
|
123 |
+
unsafe_allow_html=True,
|
124 |
+
)
|