luciagomez commited on
Commit
f623407
·
verified ·
1 Parent(s): 15b0e40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -30
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  from huggingface_hub import InferenceClient
4
  from retriever import find_similar_foundations
5
  from retriever_m3 import find_similar_foundations_api
 
6
 
7
  # -------------------------------------------------------------------
8
  # 1. Setup client for chatbot
@@ -13,31 +14,9 @@ client_chat = InferenceClient(
13
  api_key=os.environ["HF_TOKEN_inf"]
14
  )
15
 
16
- # -------------------------------------------------------------------
17
- # 2. Chatbot function
18
- # -------------------------------------------------------------------
19
- def chat_with_model(message, history):
20
- messages = []
21
- for user_msg, bot_msg in history:
22
- messages.append({"role": "user", "content": user_msg})
23
- if bot_msg:
24
- messages.append({"role": "assistant", "content": bot_msg})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = client_chat.chat.completions.create(
29
- model="mistralai/Mistral-7B-Instruct-v0.2",
30
- messages=messages,
31
- max_tokens=512,
32
- )
33
-
34
- reply = response.choices[0].message.content
35
- history.append((message, reply))
36
- return history, history
37
-
38
 
39
  # -------------------------------------------------------------------
40
- # 3. Setup client for bgem3 similarity search
41
  # -------------------------------------------------------------------
42
  client_m3 = InferenceClient(
43
  provider="hf-inference", # for embeddings similarity
@@ -45,7 +24,7 @@ client_m3 = InferenceClient(
45
  )
46
 
47
  # -------------------------------------------------------------------
48
- # 4. Foundations Retriever bge-m3 function API
49
  # -------------------------------------------------------------------
50
  def retrieve_foundations_m3(perspective, top_k=5):
51
  results = find_similar_foundations_api(perspective, client=client_m3, top_k=int(top_k))
@@ -53,7 +32,7 @@ def retrieve_foundations_m3(perspective, top_k=5):
53
 
54
 
55
  # -------------------------------------------------------------------
56
- # 5. Foundations Retriever bge-en-icl function (for UI)
57
  # -------------------------------------------------------------------
58
  def retrieve_foundations(perspective, top_k=5):
59
  """
@@ -66,20 +45,26 @@ def retrieve_foundations(perspective, top_k=5):
66
  return display_text
67
 
68
  # -------------------------------------------------------------------
69
- # 4. Gradio Interface
70
  # -------------------------------------------------------------------
71
  with gr.Blocks() as demo:
72
  gr.Markdown("# Mistral Perspective Chatbot & Foundation Finder")
73
 
74
  with gr.Tab("💬 Chatbot"):
75
- perspective = gr.Textbox(
76
- label="Enter your philanthropic perspective",
77
  placeholder="e.g. Environmental philanthropist emphasizing animal protection while fostering children's education"
78
  )
79
  chatbot = gr.Chatbot(type="messages")
80
  msg = gr.Textbox(placeholder="Ask me anything...", show_label=False)
81
- state = gr.State([]) # keeps conversation history
82
- msg.submit(chat_with_model, [msg, state], [chatbot, state])
 
 
 
 
 
 
83
 
84
  with gr.Tab("🔎 M3 Aligned Foundations"):
85
  perspective_api = gr.Textbox(label="Enter your philanthropic perspective")
 
3
  from huggingface_hub import InferenceClient
4
  from retriever import find_similar_foundations
5
  from retriever_m3 import find_similar_foundations_api
6
+ from chat import chat_with_model
7
 
8
  # -------------------------------------------------------------------
9
  # 1. Setup client for chatbot
 
14
  api_key=os.environ["HF_TOKEN_inf"]
15
  )
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  # -------------------------------------------------------------------
19
+ # 2. Setup client for bgem3 similarity search
20
  # -------------------------------------------------------------------
21
  client_m3 = InferenceClient(
22
  provider="hf-inference", # for embeddings similarity
 
24
  )
25
 
26
  # -------------------------------------------------------------------
27
+ # 3. Foundations Retriever bge-m3 function API
28
  # -------------------------------------------------------------------
29
  def retrieve_foundations_m3(perspective, top_k=5):
30
  results = find_similar_foundations_api(perspective, client=client_m3, top_k=int(top_k))
 
32
 
33
 
34
  # -------------------------------------------------------------------
35
+ # 4. Foundations Retriever bge-en-icl function (for UI)
36
  # -------------------------------------------------------------------
37
  def retrieve_foundations(perspective, top_k=5):
38
  """
 
45
  return display_text
46
 
47
  # -------------------------------------------------------------------
48
+ # 5. Gradio Interface
49
  # -------------------------------------------------------------------
50
  with gr.Blocks() as demo:
51
  gr.Markdown("# Mistral Perspective Chatbot & Foundation Finder")
52
 
53
  with gr.Tab("💬 Chatbot"):
54
+ perspective_input = gr.Textbox(
55
+ label="Enter your philanthropic perspective (optional)",
56
  placeholder="e.g. Environmental philanthropist emphasizing animal protection while fostering children's education"
57
  )
58
  chatbot = gr.Chatbot(type="messages")
59
  msg = gr.Textbox(placeholder="Ask me anything...", show_label=False)
60
+ state = gr.State([]) # stores conversation in messages format
61
+
62
+ # Streaming callback from chat.py
63
+ msg.submit(
64
+ chat_with_model,
65
+ [msg, state, perspective_input],
66
+ [chatbot, state],
67
+ )
68
 
69
  with gr.Tab("🔎 M3 Aligned Foundations"):
70
  perspective_api = gr.Textbox(label="Enter your philanthropic perspective")