fehmikaya commited on
Commit
ceb438c
·
verified ·
1 Parent(s): 0cfb831

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -7
app.py CHANGED
@@ -19,16 +19,35 @@ if API_KEY is None:
19
  # Hugging Face API settings
20
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
21
 
 
22
  headers = {
23
- "Authorization": f"Bearer {API_KEY}"
 
24
  }
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  if "messages" not in st.session_state:
27
- st.session_state.messages = [{"role": "system", "content":
28
- """
29
- You are a nice Chatbot.
30
- """
31
- }]
32
  st.session_state.messages.append({'role': 'assistant', "content": 'Ask me anything'})
33
 
34
  def handle_query(query):
@@ -46,6 +65,22 @@ def handle_query(query):
46
  else:
47
  return f"Error: Unable to fetch response from the model. Status code: {response.status_code}"
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def streamer(text):
50
  for i in text:
51
  yield i
@@ -55,7 +90,7 @@ for message in st.session_state.messages:
55
  with st.chat_message(message['role'], avatar=icons[message['role']]):
56
  st.write(message['content'])
57
 
58
- user_prompt = st.chat_input("Ask me anything:")
59
 
60
  if user_prompt:
61
  with st.chat_message("user", avatar=icons["user"]):
 
19
  # Hugging Face API settings
20
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
21
 
22
+ # Define the headers
23
  headers = {
24
+ 'Content-Type': 'application/json',
25
+ 'Authorization': f'Bearer {bearer_token}'
26
  }
27
 
28
+ def format_llama_messages(messages):
29
+ """Format messages for Llama-3 chat models.
30
+
31
+ The model only supports 'system', 'user', and 'assistant' roles, starting with 'system', then 'user', and
32
+ alternating (u/a/u/a/u...). The last message must be from 'user'.
33
+ """
34
+ prompt = []
35
+
36
+ if messages[0]["role"] == "system":
37
+ content = "".join(["<<SYS>>\n", messages[0]["content"], "\n<</SYS>>\n\n", messages[1]["content"]])
38
+ messages = [{"role": messages[1]["role"], "content": content}] + messages[2:]
39
+
40
+ for user, answer in zip(messages[::2], messages[1::2]):
41
+ prompt.extend(["<s>", "[INST] ", (user["content"]).strip(), " [/INST] ", (answer["content"]).strip(), "</s>"])
42
+
43
+ prompt.extend(["<s>", "[INST] ", (messages[-1]["content"]).strip(), " [/INST] "])
44
+
45
+ return "".join(prompt)
46
+
47
+ system_prompt = "You are a food critic Chatbot. Reply in the style of a New York Times food critic"
48
+
49
  if "messages" not in st.session_state:
50
+ st.session_state.messages = [{"role": "system", "content": system_prompt}]
 
 
 
 
51
  st.session_state.messages.append({'role': 'assistant', "content": 'Ask me anything'})
52
 
53
  def handle_query(query):
 
65
  else:
66
  return f"Error: Unable to fetch response from the model. Status code: {response.status_code}"
67
 
68
+ def handle_query(query):
69
+ st.session_state.messages.append({'role': 'user', "content": query})
70
+ formatted_messages=format_llama_messages(st.session_state.messages)
71
+ json_body = {
72
+ "inputs": formatted_messages,
73
+ "parameters": {"max_new_tokens":256, "top_p":0.9, "temperature":0.7}
74
+ }
75
+ data = json.dumps(json_body)
76
+ response = requests.request("POST", url, headers=headers, data=data)
77
+ try:
78
+ data=json.loads(response.content.decode("utf-8"))
79
+ return data[0]['generated_text'].split('[/INST] ')[1]
80
+ except:
81
+ return response
82
+
83
+
84
  def streamer(text):
85
  for i in text:
86
  yield i
 
90
  with st.chat_message(message['role'], avatar=icons[message['role']]):
91
  st.write(message['content'])
92
 
93
+ user_prompt = st.chat_input("Ask me anything about foods")
94
 
95
  if user_prompt:
96
  with st.chat_message("user", avatar=icons["user"]):