Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
from langchain.embeddings import HuggingFaceEmbeddings
|
|
@@ -13,9 +14,9 @@ huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
|
| 13 |
MODELS = [
|
| 14 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 15 |
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
|
|
|
| 16 |
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 17 |
-
"meta-llama/Meta-Llama-3.1-70B-Instruct"
|
| 18 |
-
"mistralai/Mistral-Nemo-Instruct-2407"
|
| 19 |
]
|
| 20 |
|
| 21 |
def get_embeddings():
|
|
@@ -35,7 +36,7 @@ def create_web_search_vectors(search_results):
|
|
| 35 |
documents.append(Document(page_content=content, metadata={"source": result['href']}))
|
| 36 |
return FAISS.from_documents(documents, embed)
|
| 37 |
|
| 38 |
-
def get_response_with_search(query, model, use_embeddings, num_calls=3, temperature=0.2):
|
| 39 |
search_results = duckduckgo_search(query)
|
| 40 |
|
| 41 |
if not search_results:
|
|
@@ -58,19 +59,23 @@ After writing the document, please provide a list of sources used in your respon
|
|
| 58 |
# Use Hugging Face API
|
| 59 |
client = InferenceClient(model, token=huggingface_token)
|
| 60 |
main_content = ""
|
| 61 |
-
|
| 62 |
-
for
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
logging.info(f"User Query: {message}")
|
| 75 |
logging.info(f"Model Used: {model}")
|
| 76 |
logging.info(f"Temperature: {temperature}")
|
|
@@ -78,9 +83,11 @@ def respond(message, history, model, temperature, num_calls, use_embeddings):
|
|
| 78 |
logging.info(f"Use Embeddings: {use_embeddings}")
|
| 79 |
|
| 80 |
try:
|
| 81 |
-
for main_content, sources in get_response_with_search(message, model, use_embeddings, num_calls=num_calls, temperature=temperature):
|
| 82 |
response = f"{main_content}\n\n{sources}"
|
| 83 |
yield response
|
|
|
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
logging.error(f"Error in respond function: {str(e)}")
|
| 86 |
yield f"An error occurred: {str(e)}"
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import asyncio
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 14 |
MODELS = [
|
| 15 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 16 |
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 17 |
+
"mistralai/Mistral-Nemo-Instruct-2407",
|
| 18 |
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 19 |
+
"meta-llama/Meta-Llama-3.1-70B-Instruct"
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
def get_embeddings():
|
|
|
|
| 36 |
documents.append(Document(page_content=content, metadata={"source": result['href']}))
|
| 37 |
return FAISS.from_documents(documents, embed)
|
| 38 |
|
| 39 |
+
async def get_response_with_search(query, model, use_embeddings, num_calls=3, temperature=0.2):
|
| 40 |
search_results = duckduckgo_search(query)
|
| 41 |
|
| 42 |
if not search_results:
|
|
|
|
| 59 |
# Use Hugging Face API
|
| 60 |
client = InferenceClient(model, token=huggingface_token)
|
| 61 |
main_content = ""
|
| 62 |
+
try:
|
| 63 |
+
for i in range(num_calls):
|
| 64 |
+
async for message in client.chat_completion(
|
| 65 |
+
messages=[{"role": "user", "content": prompt}],
|
| 66 |
+
max_tokens=10000,
|
| 67 |
+
temperature=temperature,
|
| 68 |
+
stream=True,
|
| 69 |
+
):
|
| 70 |
+
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
| 71 |
+
chunk = message.choices[0].delta.content
|
| 72 |
+
main_content += chunk
|
| 73 |
+
yield main_content, ""
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logging.error(f"Error in get_response_with_search: {str(e)}")
|
| 76 |
+
yield f"An error occurred while processing your request: {str(e)}", ""
|
| 77 |
+
|
| 78 |
+
async def respond(message, history, model, temperature, num_calls, use_embeddings):
|
| 79 |
logging.info(f"User Query: {message}")
|
| 80 |
logging.info(f"Model Used: {model}")
|
| 81 |
logging.info(f"Temperature: {temperature}")
|
|
|
|
| 83 |
logging.info(f"Use Embeddings: {use_embeddings}")
|
| 84 |
|
| 85 |
try:
|
| 86 |
+
async for main_content, sources in get_response_with_search(message, model, use_embeddings, num_calls=num_calls, temperature=temperature):
|
| 87 |
response = f"{main_content}\n\n{sources}"
|
| 88 |
yield response
|
| 89 |
+
except asyncio.CancelledError:
|
| 90 |
+
yield "The operation was cancelled. Please try again."
|
| 91 |
except Exception as e:
|
| 92 |
logging.error(f"Error in respond function: {str(e)}")
|
| 93 |
yield f"An error occurred: {str(e)}"
|