added Multithreading
Browse files
app.py
CHANGED
|
@@ -5,18 +5,21 @@ from modules.validation import calculate_truthfulness_score
|
|
| 5 |
from modules.knowledge_graph import search_kg
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# Load environment variables from .env file
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
-
|
| 13 |
# Constants
|
| 14 |
-
KG_INDEX_PATH="KG/news_category_index.faiss"
|
| 15 |
-
KG_DATASET_PATH="KG/News_Category_Dataset_v3.json"
|
| 16 |
SEARCH_API_KEY = os.getenv("SEARCH_API_KEY")
|
| 17 |
SEARCH_BASE_URL = os.getenv("SEARCH_BASE_URL")
|
| 18 |
SEARCH_MODEL = os.getenv("SEARCH_MODEL")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
# Function to process input and evaluate truthfulness
|
| 21 |
def evaluate_news(news_input):
|
| 22 |
# Display loading message
|
|
@@ -37,16 +40,13 @@ def evaluate_news(news_input):
|
|
| 37 |
news_text = news_input
|
| 38 |
|
| 39 |
try:
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
api_key=SEARCH_API_KEY,
|
| 47 |
-
base_url=SEARCH_BASE_URL,
|
| 48 |
-
model=SEARCH_MODEL
|
| 49 |
-
)
|
| 50 |
|
| 51 |
# Combine context from KG and online search
|
| 52 |
context = online_search_results['message_content'] + '\n' + kg_content
|
|
@@ -98,4 +98,4 @@ with gr.Blocks() as demo:
|
|
| 98 |
)
|
| 99 |
|
| 100 |
# Launch Gradio App
|
| 101 |
-
demo.launch()
|
|
|
|
| 5 |
from modules.knowledge_graph import search_kg
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
| 8 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
|
| 10 |
# Load environment variables from .env file
|
| 11 |
load_dotenv()
|
| 12 |
|
|
|
|
| 13 |
# Constants
|
| 14 |
+
KG_INDEX_PATH = "KG/news_category_index.faiss"
|
| 15 |
+
KG_DATASET_PATH = "KG/News_Category_Dataset_v3.json"
|
| 16 |
SEARCH_API_KEY = os.getenv("SEARCH_API_KEY")
|
| 17 |
SEARCH_BASE_URL = os.getenv("SEARCH_BASE_URL")
|
| 18 |
SEARCH_MODEL = os.getenv("SEARCH_MODEL")
|
| 19 |
|
| 20 |
+
# Initialize ThreadPoolExecutor
|
| 21 |
+
executor = ThreadPoolExecutor(max_workers=2)
|
| 22 |
+
|
| 23 |
# Function to process input and evaluate truthfulness
|
| 24 |
def evaluate_news(news_input):
|
| 25 |
# Display loading message
|
|
|
|
| 40 |
news_text = news_input
|
| 41 |
|
| 42 |
try:
|
| 43 |
+
# Run both search functions concurrently using ThreadPoolExecutor
|
| 44 |
+
future_kg = executor.submit(search_kg, news_text, KG_INDEX_PATH, KG_DATASET_PATH)
|
| 45 |
+
future_online = executor.submit(search_online, news_text, SEARCH_API_KEY, SEARCH_BASE_URL, SEARCH_MODEL)
|
| 46 |
|
| 47 |
+
# Wait for the results of both tasks
|
| 48 |
+
kg_content = future_kg.result()
|
| 49 |
+
online_search_results = future_online.result()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Combine context from KG and online search
|
| 52 |
context = online_search_results['message_content'] + '\n' + kg_content
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
# Launch Gradio App
|
| 101 |
+
demo.launch()
|