luciagomez commited on
Commit
0ba1ae3
·
verified ·
1 Parent(s): 4dad5ce

create app.py with perspective taking, chatbot, and foundationretriever

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+ from retriever import find_similar_foundations
5
+
6
+ # -------------------------------------------------------------------
7
+ # 1. Setup client for chatbot
8
+ # -------------------------------------------------------------------
9
+ # Use my token stored as a Space secret for inference
10
+ client = InferenceClient(
11
+ provider="featherless-ai",
12
+ api_key=os.environ["HF_TOKEN"]
13
+ )
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.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. Foundations Retriever function (for UI)
41
+ # -------------------------------------------------------------------
42
+ def retrieve_foundations(query, top_k):
43
+ results = find_similar_foundations(query, top_k=top_k)
44
+ output = "\n\n".join(
45
+ [f"**{r['rank']}. {r['title']}**\n{r['purpose']}\n(similarity: {r['similarity']:.4f})"
46
+ for r in results]
47
+ )
48
+ return output
49
+
50
+
51
+ # -------------------------------------------------------------------
52
+ # 4. Gradio Interface
53
+ # -------------------------------------------------------------------
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown("# Mistral Perspective Chatbot & Foundation Finder")
56
+
57
+ with gr.Tab("💬 Chatbot"):
58
+ chatbot = gr.Chatbot()
59
+ msg = gr.Textbox(placeholder="Ask me anything...", show_label=False)
60
+ state = gr.State([]) # keeps conversation history
61
+ msg.submit(chat_with_model, [msg, state], [chatbot, state])
62
+
63
+
64
+ with gr.Tab("🔎 Find Aligned Foundations"):
65
+ perspective = gr.Textbox(
66
+ label="Enter your philanthropic perspective",
67
+ placeholder="e.g. Environmental philanthropist emphasizing animal protection while fostering children's education"
68
+ )
69
+ top_k = gr.Slider(1, 5, value=2, step=1, label="Number of results")
70
+ output = gr.Dataframe(headers=["Title", "Purpose", "similarity"], wrap=True)
71
+
72
+ btn = gr.Button("Find Foundations")
73
+ btn.click(fn=retrieve_foundations, inputs=[perspective, top_k], outputs=output)
74
+
75
+ demo.launch(server_name="0.0.0.0", server_port=7860)