Spaces:
Runtime error
Runtime error
Sanyam0605
commited on
Commit
β’
15a77b6
1
Parent(s):
ed3e985
Upload 9 files
Browse files- Hugchat x Metaphor/.idea/.gitignore +3 -0
- Hugchat x Metaphor/.idea/Hugchat x Metaphor.iml +8 -0
- Hugchat x Metaphor/.idea/inspectionProfiles/Project_Default.xml +40 -0
- Hugchat x Metaphor/.idea/inspectionProfiles/profiles_settings.xml +6 -0
- Hugchat x Metaphor/.idea/misc.xml +4 -0
- Hugchat x Metaphor/.idea/modules.xml +8 -0
- Hugchat x Metaphor/.idea/workspace.xml +61 -0
- Hugchat x Metaphor/.streamlit/secrets.toml +9 -0
- Hugchat x Metaphor/main.py +97 -0
Hugchat x Metaphor/.idea/.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
Hugchat x Metaphor/.idea/Hugchat x Metaphor.iml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$" />
|
5 |
+
<orderEntry type="jdk" jdkName="Desktop" jdkType="Python SDK" />
|
6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
7 |
+
</component>
|
8 |
+
</module>
|
Hugchat x Metaphor/.idea/inspectionProfiles/Project_Default.xml
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<profile version="1.0">
|
3 |
+
<option name="myName" value="Project Default" />
|
4 |
+
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
5 |
+
<option name="ignoredPackages">
|
6 |
+
<value>
|
7 |
+
<list size="5">
|
8 |
+
<item index="0" class="java.lang.String" itemvalue="scikit_learn" />
|
9 |
+
<item index="1" class="java.lang.String" itemvalue="librosa" />
|
10 |
+
<item index="2" class="java.lang.String" itemvalue="accelerate" />
|
11 |
+
<item index="3" class="java.lang.String" itemvalue="datasets" />
|
12 |
+
<item index="4" class="java.lang.String" itemvalue="evaluate" />
|
13 |
+
</list>
|
14 |
+
</value>
|
15 |
+
</option>
|
16 |
+
</inspection_tool>
|
17 |
+
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
18 |
+
<option name="ignoredErrors">
|
19 |
+
<list>
|
20 |
+
<option value="E265" />
|
21 |
+
</list>
|
22 |
+
</option>
|
23 |
+
</inspection_tool>
|
24 |
+
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
25 |
+
<option name="ignoredErrors">
|
26 |
+
<list>
|
27 |
+
<option value="N802" />
|
28 |
+
</list>
|
29 |
+
</option>
|
30 |
+
</inspection_tool>
|
31 |
+
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
32 |
+
<option name="ignoredIdentifiers">
|
33 |
+
<list>
|
34 |
+
<option value="object.__getitem__" />
|
35 |
+
<option value="streamlit.timeline" />
|
36 |
+
</list>
|
37 |
+
</option>
|
38 |
+
</inspection_tool>
|
39 |
+
</profile>
|
40 |
+
</component>
|
Hugchat x Metaphor/.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
Hugchat x Metaphor/.idea/misc.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Desktop" project-jdk-type="Python SDK" />
|
4 |
+
</project>
|
Hugchat x Metaphor/.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/Hugchat x Metaphor.iml" filepath="$PROJECT_DIR$/.idea/Hugchat x Metaphor.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
Hugchat x Metaphor/.idea/workspace.xml
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="AutoImportSettings">
|
4 |
+
<option name="autoReloadType" value="SELECTIVE" />
|
5 |
+
</component>
|
6 |
+
<component name="ChangeListManager">
|
7 |
+
<list default="true" id="6a6530b3-ef96-42ef-bc83-7365afb9f765" name="Changes" comment="" />
|
8 |
+
<option name="SHOW_DIALOG" value="false" />
|
9 |
+
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
10 |
+
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
11 |
+
<option name="LAST_RESOLUTION" value="IGNORE" />
|
12 |
+
</component>
|
13 |
+
<component name="MarkdownSettingsMigration">
|
14 |
+
<option name="stateVersion" value="1" />
|
15 |
+
</component>
|
16 |
+
<component name="ProjectId" id="2W85JnaY0yX2mQLKtwp452a6J5L" />
|
17 |
+
<component name="ProjectViewState">
|
18 |
+
<option name="hideEmptyMiddlePackages" value="true" />
|
19 |
+
<option name="showLibraryContents" value="true" />
|
20 |
+
</component>
|
21 |
+
<component name="PropertiesComponent"><![CDATA[{
|
22 |
+
"keyToString": {
|
23 |
+
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
24 |
+
"RunOnceActivity.ShowReadmeOnStart": "true"
|
25 |
+
}
|
26 |
+
}]]></component>
|
27 |
+
<component name="RunManager">
|
28 |
+
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
29 |
+
<module name="Hugchat x Metaphor" />
|
30 |
+
<option name="INTERPRETER_OPTIONS" value="" />
|
31 |
+
<option name="PARENT_ENVS" value="true" />
|
32 |
+
<envs>
|
33 |
+
<env name="PYTHONUNBUFFERED" value="1" />
|
34 |
+
</envs>
|
35 |
+
<option name="SDK_HOME" value="" />
|
36 |
+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
37 |
+
<option name="IS_MODULE_SDK" value="true" />
|
38 |
+
<option name="ADD_CONTENT_ROOTS" value="true" />
|
39 |
+
<option name="ADD_SOURCE_ROOTS" value="true" />
|
40 |
+
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
|
41 |
+
<option name="PARAMETERS" value="" />
|
42 |
+
<option name="SHOW_COMMAND_LINE" value="false" />
|
43 |
+
<option name="EMULATE_TERMINAL" value="false" />
|
44 |
+
<option name="MODULE_MODE" value="false" />
|
45 |
+
<option name="REDIRECT_INPUT" value="false" />
|
46 |
+
<option name="INPUT_FILE" value="" />
|
47 |
+
<method v="2" />
|
48 |
+
</configuration>
|
49 |
+
</component>
|
50 |
+
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
51 |
+
<component name="TaskManager">
|
52 |
+
<task active="true" id="Default" summary="Default task">
|
53 |
+
<changelist id="6a6530b3-ef96-42ef-bc83-7365afb9f765" name="Changes" comment="" />
|
54 |
+
<created>1696101297829</created>
|
55 |
+
<option name="number" value="Default" />
|
56 |
+
<option name="presentableId" value="Default" />
|
57 |
+
<updated>1696101297829</updated>
|
58 |
+
</task>
|
59 |
+
<servers />
|
60 |
+
</component>
|
61 |
+
</project>
|
Hugchat x Metaphor/.streamlit/secrets.toml
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
primaryColor="#60b4ff"
|
3 |
+
backgroundColor="#FFFFFF"
|
4 |
+
secondaryBackgroundColor="#F0F2F6"
|
5 |
+
textColor="#262730"
|
6 |
+
font="sans serif"
|
7 |
+
|
8 |
+
[server]
|
9 |
+
enableStaticServing = true
|
Hugchat x Metaphor/main.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)
|