File size: 2,798 Bytes
0ba1ae3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import gradio as gr
from huggingface_hub import InferenceClient
from retriever import find_similar_foundations

# -------------------------------------------------------------------
# 1. Setup client for chatbot
# -------------------------------------------------------------------
# Use my token stored as a Space secret for inference
client = InferenceClient(
    provider="featherless-ai",
    api_key=os.environ["HF_TOKEN"]
)


# -------------------------------------------------------------------
# 2. Chatbot function
# -------------------------------------------------------------------
def chat_with_model(message, history):
    messages = []
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        if bot_msg:
            messages.append({"role": "assistant", "content": bot_msg})

    messages.append({"role": "user", "content": message})

    response = client.chat.completions.create(
        model="mistralai/Mistral-7B-Instruct-v0.2",
        messages=messages,
        max_tokens=512,
    )

    reply = response.choices[0].message.content
    history.append((message, reply))
    return history, history


# -------------------------------------------------------------------
# 3. Foundations Retriever function (for UI)
# -------------------------------------------------------------------
def retrieve_foundations(query, top_k):
    results = find_similar_foundations(query, top_k=top_k)
    output = "\n\n".join(
        [f"**{r['rank']}. {r['title']}**\n{r['purpose']}\n(similarity: {r['similarity']:.4f})"
         for r in results]
    )
    return output


# -------------------------------------------------------------------
# 4. Gradio Interface
# -------------------------------------------------------------------
with gr.Blocks() as demo:
    gr.Markdown("# Mistral Perspective Chatbot & Foundation Finder")

    with gr.Tab("💬 Chatbot"):
        chatbot = gr.Chatbot()
        msg = gr.Textbox(placeholder="Ask me anything...", show_label=False)
        state = gr.State([])  # keeps conversation history
        msg.submit(chat_with_model, [msg, state], [chatbot, state])


    with gr.Tab("🔎 Find Aligned Foundations"):
        perspective = gr.Textbox(
            label="Enter your philanthropic perspective",
            placeholder="e.g. Environmental philanthropist emphasizing animal protection while fostering children's education"
        )
        top_k = gr.Slider(1, 5, value=2, step=1, label="Number of results")
        output = gr.Dataframe(headers=["Title", "Purpose", "similarity"], wrap=True)

        btn = gr.Button("Find Foundations")
        btn.click(fn=retrieve_foundations, inputs=[perspective, top_k], outputs=output)

demo.launch(server_name="0.0.0.0", server_port=7860)