Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import logging
|
|
|
|
| 4 |
|
| 5 |
# Configure logging
|
| 6 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -61,6 +62,16 @@ def query(payload, api_url):
|
|
| 61 |
logger.error(f"Failed to decode JSON response: {response.text}")
|
| 62 |
return None
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# Chat interface
|
| 65 |
st.title("🤖 DeepSeek Chatbot")
|
| 66 |
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
|
@@ -79,9 +90,19 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 79 |
|
| 80 |
try:
|
| 81 |
with st.spinner("Generating response..."):
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
payload = {
|
| 86 |
"inputs": full_prompt,
|
| 87 |
"parameters": {
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import logging
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
# Configure logging
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 62 |
logger.error(f"Failed to decode JSON response: {response.text}")
|
| 63 |
return None
|
| 64 |
|
| 65 |
+
# Function to perform Google search using SerpHouse API
|
| 66 |
+
def search_web(query):
|
| 67 |
+
search_url = f"https://serphouse.com/api/v1/google_custom_search?api_key=V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6&q={query}&num=5"
|
| 68 |
+
response = requests.get(search_url)
|
| 69 |
+
if response.status_code == 200:
|
| 70 |
+
return response.json()
|
| 71 |
+
else:
|
| 72 |
+
logger.error(f"Error while searching: {response.status_code}")
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
# Chat interface
|
| 76 |
st.title("🤖 DeepSeek Chatbot")
|
| 77 |
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
|
|
|
| 90 |
|
| 91 |
try:
|
| 92 |
with st.spinner("Generating response..."):
|
| 93 |
+
# Perform a web search based on the user input
|
| 94 |
+
search_results = search_web(prompt)
|
| 95 |
+
|
| 96 |
+
# Process search results if available
|
| 97 |
+
if search_results and "items" in search_results:
|
| 98 |
+
search_content = "\n".join([f"**{item['title']}**: {item['snippet']}" for item in search_results["items"]])
|
| 99 |
+
search_content = f"Here are some search results related to your question:\n\n{search_content}\n\n"
|
| 100 |
+
|
| 101 |
+
# Combine the system message, search results, and user input into a single prompt
|
| 102 |
+
full_prompt = f"{system_message}\n\n{search_content}User: {prompt}\nAssistant:"
|
| 103 |
+
else:
|
| 104 |
+
full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
|
| 105 |
+
|
| 106 |
payload = {
|
| 107 |
"inputs": full_prompt,
|
| 108 |
"parameters": {
|