Spaces:
Runtime error
Runtime error
Sanyam0605
commited on
Commit
β’
502a217
1
Parent(s):
15a77b6
Create app.py
Browse files- Hugchat x Metaphor/app.py +97 -0
Hugchat x Metaphor/app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from hugchat import hugchat
|
3 |
+
from hugchat.login import Login
|
4 |
+
from metaphor_python import Metaphor
|
5 |
+
|
6 |
+
|
7 |
+
# App title
|
8 |
+
st.set_page_config(page_title="HugChat with Metaphor")
|
9 |
+
|
10 |
+
# Define Metaphor API key
|
11 |
+
METAPHOR_API_KEY = "1cd6d71b-e530-4ea3-bb18-e9599e641f66" # Replace with your Metaphor API key
|
12 |
+
with st.sidebar:
|
13 |
+
st.title('π€π¬ HugChat x Metaphor')
|
14 |
+
if ('EMAIL' in st.secrets) and ('PASS' in st.secrets):
|
15 |
+
st.success('HuggingFace Login credentials already provided!', icon='β
')
|
16 |
+
hf_email = st.secrets['EMAIL']
|
17 |
+
hf_pass = st.secrets['PASS']
|
18 |
+
else:
|
19 |
+
hf_email = st.text_input('Enter E-mail:', type='password')
|
20 |
+
hf_pass = st.text_input('Enter password:', type='password')
|
21 |
+
if not (hf_email and hf_pass):
|
22 |
+
st.warning('Please enter your credentials!', icon='β οΈ')
|
23 |
+
else:
|
24 |
+
st.success('Proceed to entering your prompt message!', icon='π')
|
25 |
+
|
26 |
+
# Create Metaphor client
|
27 |
+
metaphor = Metaphor(METAPHOR_API_KEY)
|
28 |
+
|
29 |
+
# Store LLM generated responses
|
30 |
+
if "messages" not in st.session_state:
|
31 |
+
st.session_state.messages = [{"role": "assistant", "content": "Heya Metaphor bot this side, how may i assist ?"}]
|
32 |
+
|
33 |
+
# Display or clear chat messages
|
34 |
+
for message in st.session_state.messages:
|
35 |
+
with st.chat_message(message["role"]):
|
36 |
+
st.write(message["content"])
|
37 |
+
|
38 |
+
def clear_chat_history():
|
39 |
+
st.session_state.messages = [{"role": "assistant", "content": "Heya Metaphor bot this side, how may i assist?"}]
|
40 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
41 |
+
|
42 |
+
# Function for generating LLM response
|
43 |
+
def generate_response(prompt_input, email, passwd):
|
44 |
+
# Hugging Face Login
|
45 |
+
sign = Login(email, passwd)
|
46 |
+
cookies = sign.login()
|
47 |
+
|
48 |
+
# Create ChatBot
|
49 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
50 |
+
|
51 |
+
# Check if the user's input is a specific question
|
52 |
+
if prompt_input.strip().lower() in ["who are you?", "who made you?"]:
|
53 |
+
response = "I am an AI LLama Hugchat of Huggingface which is integrated with Metaphor in the backend."
|
54 |
+
else:
|
55 |
+
# Fetch Metaphor search results
|
56 |
+
search_options = {
|
57 |
+
"query": prompt_input,
|
58 |
+
"num_results": 5 # You can adjust the number of results as needed
|
59 |
+
}
|
60 |
+
try:
|
61 |
+
search_response = metaphor.search(**search_options)
|
62 |
+
|
63 |
+
# Extract links and summaries from the Metaphor search results
|
64 |
+
links_and_summaries = [
|
65 |
+
f"Title: {result.title}\nURL: {result.url}\nSummary: {result.extract}\n---"
|
66 |
+
for result in search_response.results
|
67 |
+
]
|
68 |
+
|
69 |
+
# Combine the user's query and Metaphor output with the previous conversation
|
70 |
+
string_dialogue = "You are a helpful assistant."
|
71 |
+
for dict_message in st.session_state.messages:
|
72 |
+
if dict_message["role"] == "user":
|
73 |
+
string_dialogue += "User: " + dict_message["content"] + "\n\n"
|
74 |
+
else:
|
75 |
+
string_dialogue += "Assistant: " + dict_message["content"] + "\n\n"
|
76 |
+
|
77 |
+
prompt = f"{string_dialogue}\n{prompt_input}\n{''.join(links_and_summaries)}\n Assistant: "
|
78 |
+
response = chatbot.chat(prompt)
|
79 |
+
except Exception as e:
|
80 |
+
response = str(e)
|
81 |
+
|
82 |
+
return response
|
83 |
+
# User-provided prompt
|
84 |
+
if prompt := st.chat_input(disabled=not (hf_email and hf_pass)):
|
85 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
86 |
+
with st.chat_message("user"):
|
87 |
+
st.write(prompt)
|
88 |
+
|
89 |
+
# Generate a new response if the last message is not from the assistant
|
90 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
91 |
+
with st.chat_message("assistant"):
|
92 |
+
with st.spinner("Thinking..."):
|
93 |
+
response = generate_response(prompt, hf_email, hf_pass)
|
94 |
+
st.write(response)
|
95 |
+
|
96 |
+
message = {"role": "assistant", "content": response}
|
97 |
+
st.session_state.messages.append(message)
|