Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from azure.search.documents import SearchClient
|
| 3 |
from azure.search.documents.models import QueryType
|
|
|
|
| 4 |
import requests
|
| 5 |
|
| 6 |
# Azure Configuration
|
|
@@ -12,7 +13,7 @@ OPENAI_KEY = "B1XyCaz87o456EVD949oODcGC8KTAEQsNLI7Yq5cnYKk41SMY9PtJQQJ99AKACHYHv
|
|
| 12 |
DEPLOYMENT_ID = "gpt-4"
|
| 13 |
|
| 14 |
# Azure Cognitive Search Client
|
| 15 |
-
search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=SEARCH_KEY)
|
| 16 |
|
| 17 |
# Function to call Azure OpenAI API
|
| 18 |
def call_openai_api(prompt):
|
|
@@ -29,25 +30,27 @@ def call_openai_api(prompt):
|
|
| 29 |
response = requests.post(url, headers=headers, json=data)
|
| 30 |
if response.status_code == 200:
|
| 31 |
return response.json()["choices"][0]["text"].strip()
|
| 32 |
-
return "
|
| 33 |
|
| 34 |
# Function to process user queries
|
| 35 |
def process_query(question):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Predefined questions
|
| 53 |
predefined_questions = [
|
|
@@ -60,7 +63,7 @@ predefined_questions = [
|
|
| 60 |
|
| 61 |
# Gradio UI
|
| 62 |
def chatbot_ui(question):
|
| 63 |
-
if question
|
| 64 |
response = process_query(question)
|
| 65 |
return response
|
| 66 |
return "Please select a predefined question or enter a valid query."
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from azure.search.documents import SearchClient
|
| 3 |
from azure.search.documents.models import QueryType
|
| 4 |
+
from azure.core.credentials import AzureKeyCredential
|
| 5 |
import requests
|
| 6 |
|
| 7 |
# Azure Configuration
|
|
|
|
| 13 |
DEPLOYMENT_ID = "gpt-4"
|
| 14 |
|
| 15 |
# Azure Cognitive Search Client
|
| 16 |
+
search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=AzureKeyCredential(SEARCH_KEY))
|
| 17 |
|
| 18 |
# Function to call Azure OpenAI API
|
| 19 |
def call_openai_api(prompt):
|
|
|
|
| 30 |
response = requests.post(url, headers=headers, json=data)
|
| 31 |
if response.status_code == 200:
|
| 32 |
return response.json()["choices"][0]["text"].strip()
|
| 33 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 34 |
|
| 35 |
# Function to process user queries
|
| 36 |
def process_query(question):
|
| 37 |
+
try:
|
| 38 |
+
# Search in Azure Cognitive Search
|
| 39 |
+
search_results = search_client.search(
|
| 40 |
+
search_text=question,
|
| 41 |
+
query_type=QueryType.SIMPLE,
|
| 42 |
+
top=3
|
| 43 |
+
)
|
| 44 |
+
# Extract search results content
|
| 45 |
+
content = "\n".join([result['content'] for result in search_results])
|
| 46 |
+
if not content:
|
| 47 |
+
return "No relevant information found in the indexed documents."
|
| 48 |
+
|
| 49 |
+
# Use OpenAI to generate an enhanced response
|
| 50 |
+
prompt = f"Use the following content to answer the question:\n{content}\nQuestion: {question}"
|
| 51 |
+
return call_openai_api(prompt)
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Error during query processing: {e}"
|
| 54 |
|
| 55 |
# Predefined questions
|
| 56 |
predefined_questions = [
|
|
|
|
| 63 |
|
| 64 |
# Gradio UI
|
| 65 |
def chatbot_ui(question):
|
| 66 |
+
if question:
|
| 67 |
response = process_query(question)
|
| 68 |
return response
|
| 69 |
return "Please select a predefined question or enter a valid query."
|