freshbash commited on
Commit
a2946a5
·
1 Parent(s): 695ae2d

using keras model

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -1,11 +1,20 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
- client = InferenceClient("bhashwarsengupta/gemma-finance")
8
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def respond(
11
  message,
@@ -15,29 +24,27 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
  for val in history:
21
  if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
  if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
29
 
30
- for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
33
- stream=True,
34
  temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
41
 
42
 
43
  """
 
1
  import gradio as gr
2
+ import keras_nlp
3
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
 
7
 
8
+ model = keras_nlp.models.GemmaCausalLM.from_preset("kaggle://bhashwar22/gemma-for-finance/keras/gemma-for-finance")
9
+ print("model successfully loaded!")
10
+
11
+ context = """You are an intelligent personal finance assistant
12
+ designed to help users understand various financial concepts.
13
+ You are supposed to provide concise and easy-to-understand explanations for the requested questions,
14
+ ensuring the users feel informed and confident about managing their money.
15
+ Keep your answers limited to 50-100 words.
16
+ If you receive any non-finance related query, please return the following response:
17
+ \"Unrelated Topic\""""
18
 
19
  def respond(
20
  message,
 
24
  temperature,
25
  top_p,
26
  ):
27
+ messages = f"Context: {context}\n"
28
 
29
  for val in history:
30
  if val[0]:
31
+ messages += f"Question: {val[0]}\n"
32
  if val[1]:
33
+ messages += f"Answer: {val[1]}\n"
 
 
34
 
35
+ messages += f"Question: {message}\nAnswer: "
36
 
37
+ output = model.generate(
38
  messages,
39
  max_tokens=max_tokens,
 
40
  temperature=temperature,
41
+ top_p=top_o
42
+ )
 
43
 
44
+ # Split by "Answer:" from the right and get the last part
45
+ response = output.rsplit("Answer: ", 1)[-1]
46
+
47
+ return response
48
 
49
 
50
  """