andreska commited on
Commit
d943ab1
·
verified ·
1 Parent(s): ec395fa

Moved checkbox up, added possibility to press enter to submit

Browse files
Files changed (1) hide show
  1. app.py +40 -32
app.py CHANGED
@@ -3,58 +3,66 @@ import streamlit as st
3
  from datasets import load_dataset
4
  from huggingface_hub import InferenceClient
5
 
 
6
  api_key = os.getenv("HF_API_KEY")
7
  client = InferenceClient(api_key=api_key)
 
 
8
  dataset = load_dataset("andreska/adregadocs", split="test")
9
 
10
- st.markdown(
11
- """
12
- <style>
13
- .sdMain {
14
- background-color: yellow;
15
- }
16
- </style>
17
- """,
18
- unsafe_allow_html=True )
19
-
20
- def read_dataset(dataset):
21
- text = []
22
- for item in dataset:
23
  text.append(item['text'])
24
- return "\n".join(text)
25
 
26
  context = read_dataset(dataset)
27
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  st.title("Adrega AI Help")
29
- user_input = st.text_input('Ask me a question')
30
  include_context = st.checkbox('Search in Help')
31
 
32
- if 'conversation' not in st.session_state:
33
  st.session_state.conversation = ""
34
 
35
- if st.button("Submit"):
36
- if user_input:
37
- if include_context:
38
- messages = [
39
- {"role": "system", "content": f"Context: {context}"},
40
- {"role": "user", "content": user_input}
41
- ]
42
- else: messages = [
43
- {"role": "user", "content": user_input}
44
- ]
 
 
45
 
46
  completion = client.chat.completions.create(
47
- model= "Qwen/Qwen2.5-72B-Instruct",
48
- #model="Qwen/Qwen2.5-Coder-32B-Instruct",
49
  #model="HuggingFaceTB/SmolLM2-1.7B-Instruct",
50
- messages=messages,
51
- max_tokens=500
52
  )
53
 
54
  answer = completion.choices[0].message['content']
55
  st.session_state.conversation += f"User: {user_input}\nAdrega AI: {answer}\n\n"
56
- st.write(f"Adrega AI: {answer}")
57
- else:
58
  st.write("Please enter a question.")
59
 
 
60
  st.text_area("Conversation History", value=st.session_state.conversation, height=300, max_chars=None)
 
3
  from datasets import load_dataset
4
  from huggingface_hub import InferenceClient
5
 
6
+ # Get the API key from the environment variable
7
  api_key = os.getenv("HF_API_KEY")
8
  client = InferenceClient(api_key=api_key)
9
+
10
+ # Load the dataset
11
  dataset = load_dataset("andreska/adregadocs", split="test")
12
 
13
+ # Function to read the content from the dataset
14
+ def read_dataset(dataset):
15
+ text = []
16
+ for item in dataset:
 
 
 
 
 
 
 
 
 
17
  text.append(item['text'])
18
+ return "\n.join(text)
19
 
20
  context = read_dataset(dataset)
21
 
22
+ # Inject custom CSS to change the background color to yellow
23
+ st.markdown(
24
+ """
25
+ <style>
26
+ body {
27
+ background-color: yellow;
28
+ }
29
+ </style>
30
+ """,
31
+ unsafe_allow_html=True
32
+ )
33
+
34
  st.title("Adrega AI Help")
 
35
  include_context = st.checkbox('Search in Help')
36
 
37
+ if 'conversation' not in st.session_state:
38
  st.session_state.conversation = ""
39
 
40
+ def handle_submit():
41
+ user_input = st.session_state.user_input
42
+ if user_input:
43
+ if st.session_state.include_context:
44
+ messages = [
45
+ {"role": "system", "content": f"Context: {context}"},
46
+ {"role": "user", "content": user_input}
47
+ ]
48
+ else:
49
+ messages = [
50
+ {"role": "user", "content": user_input}
51
+ ]
52
 
53
  completion = client.chat.completions.create(
54
+ model="Qwen/Qwen2.5-72B-Instruct",
55
+ #model="Qwen/Qwen2.5-Coder-32B-Instruct",
56
  #model="HuggingFaceTB/SmolLM2-1.7B-Instruct",
57
+ messages=messages,
58
+ max_tokens=500
59
  )
60
 
61
  answer = completion.choices[0].message['content']
62
  st.session_state.conversation += f"User: {user_input}\nAdrega AI: {answer}\n\n"
63
+ #st.write(f"Adrega AI: {answer}")
64
+ else:
65
  st.write("Please enter a question.")
66
 
67
+ st.text_input('Ask me a question', key='user_input', on_change=handle_submit)
68
  st.text_area("Conversation History", value=st.session_state.conversation, height=300, max_chars=None)