Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ import gradio as gr
|
|
| 3 |
from azure.storage.blob import BlobServiceClient
|
| 4 |
from azure.search.documents import SearchClient
|
| 5 |
from azure.search.documents.models import QueryType
|
| 6 |
-
from azure.
|
| 7 |
-
from azure.
|
| 8 |
|
| 9 |
# Azure Configuration
|
| 10 |
SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
|
|
@@ -16,55 +16,56 @@ BLOB_CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=blobstorage
|
|
| 16 |
BLOB_CONTAINER_NAME = "uploaded-files"
|
| 17 |
|
| 18 |
# Azure Clients
|
| 19 |
-
search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=
|
| 20 |
-
openai_client = OpenAIClient(credential=AzureKeyCredential(OPENAI_KEY), endpoint=OPENAI_ENDPOINT)
|
| 21 |
blob_service_client = BlobServiceClient.from_connection_string(BLOB_CONNECTION_STRING)
|
|
|
|
| 22 |
|
| 23 |
# Function: Process User Query
|
| 24 |
-
def process_query(question):
|
| 25 |
-
#
|
| 26 |
search_results = search_client.search(
|
| 27 |
search_text=question,
|
| 28 |
query_type=QueryType.SIMPLE,
|
| 29 |
top=3
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
content = "\n".join([result["content"] for result in search_results])
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
openai_response = openai_client.
|
| 36 |
deployment_id="gpt-4",
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
max_tokens=500,
|
| 39 |
temperature=0.7
|
| 40 |
)
|
| 41 |
-
return openai_response.choices[0].text.strip()
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
def upload_pdfs(file_list):
|
| 45 |
container_client = blob_service_client.get_container_client(BLOB_CONTAINER_NAME)
|
| 46 |
|
| 47 |
for file in file_list:
|
| 48 |
-
# Upload each file to Blob Storage
|
| 49 |
blob_name = os.path.basename(file.name)
|
| 50 |
blob_client = container_client.get_blob_client(blob_name)
|
| 51 |
-
blob_client.upload_blob(file
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
def chatbot_ui(question, files
|
| 58 |
if files:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
return upload_response
|
| 62 |
|
| 63 |
-
|
| 64 |
-
response = process_query(question)
|
| 65 |
return response
|
| 66 |
|
| 67 |
-
# Gradio
|
| 68 |
demo = gr.Interface(
|
| 69 |
fn=chatbot_ui,
|
| 70 |
inputs=[
|
|
@@ -72,8 +73,7 @@ demo = gr.Interface(
|
|
| 72 |
gr.File(label="Upload PDFs (optional)", file_types=[".pdf"], file_count="multiple")
|
| 73 |
],
|
| 74 |
outputs="text",
|
| 75 |
-
title="Azure RAG Chatbot"
|
| 76 |
-
description="Upload PDFs or ask questions based on the indexed documents in Azure Cognitive Search."
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from azure.storage.blob import BlobServiceClient
|
| 4 |
from azure.search.documents import SearchClient
|
| 5 |
from azure.search.documents.models import QueryType
|
| 6 |
+
from azure.openai import OpenAIClient
|
| 7 |
+
from azure.identity import DefaultAzureCredential
|
| 8 |
|
| 9 |
# Azure Configuration
|
| 10 |
SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
|
|
|
|
| 16 |
BLOB_CONTAINER_NAME = "uploaded-files"
|
| 17 |
|
| 18 |
# Azure Clients
|
| 19 |
+
search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=SEARCH_KEY)
|
|
|
|
| 20 |
blob_service_client = BlobServiceClient.from_connection_string(BLOB_CONNECTION_STRING)
|
| 21 |
+
openai_client = OpenAIClient(endpoint=OPENAI_ENDPOINT, credential=DefaultAzureCredential())
|
| 22 |
|
| 23 |
# Function: Process User Query
|
| 24 |
+
def process_query(question, history):
|
| 25 |
+
# Azure Cognitive Search
|
| 26 |
search_results = search_client.search(
|
| 27 |
search_text=question,
|
| 28 |
query_type=QueryType.SIMPLE,
|
| 29 |
top=3
|
| 30 |
)
|
| 31 |
+
content = "\n".join([result['content'] for result in search_results])
|
|
|
|
| 32 |
|
| 33 |
+
# Azure OpenAI
|
| 34 |
+
openai_response = openai_client.chat_completions.create(
|
| 35 |
deployment_id="gpt-4",
|
| 36 |
+
messages=[
|
| 37 |
+
{"role": "system", "content": "You are an intelligent assistant."},
|
| 38 |
+
{"role": "user", "content": f"Use the following content to answer the question:\n{content}\nQuestion: {question}"}
|
| 39 |
+
],
|
| 40 |
max_tokens=500,
|
| 41 |
temperature=0.7
|
| 42 |
)
|
|
|
|
| 43 |
|
| 44 |
+
return openai_response.choices[0].message['content'].strip()
|
| 45 |
+
|
| 46 |
+
# Function: Upload PDFs and Reindex
|
| 47 |
def upload_pdfs(file_list):
|
| 48 |
container_client = blob_service_client.get_container_client(BLOB_CONTAINER_NAME)
|
| 49 |
|
| 50 |
for file in file_list:
|
|
|
|
| 51 |
blob_name = os.path.basename(file.name)
|
| 52 |
blob_client = container_client.get_blob_client(blob_name)
|
| 53 |
+
blob_client.upload_blob(file)
|
| 54 |
|
| 55 |
+
# Trigger reindexing (update indexer in Azure Cognitive Search)
|
| 56 |
+
# Note: This assumes an indexer is already configured in the Azure portal.
|
| 57 |
+
return "PDFs uploaded and reindexed successfully!"
|
| 58 |
|
| 59 |
+
# Gradio UI
|
| 60 |
+
def chatbot_ui(question, files):
|
| 61 |
if files:
|
| 62 |
+
upload_pdfs(files)
|
| 63 |
+
return "Files uploaded. Please ask your question again."
|
|
|
|
| 64 |
|
| 65 |
+
response = process_query(question, [])
|
|
|
|
| 66 |
return response
|
| 67 |
|
| 68 |
+
# Gradio App
|
| 69 |
demo = gr.Interface(
|
| 70 |
fn=chatbot_ui,
|
| 71 |
inputs=[
|
|
|
|
| 73 |
gr.File(label="Upload PDFs (optional)", file_types=[".pdf"], file_count="multiple")
|
| 74 |
],
|
| 75 |
outputs="text",
|
| 76 |
+
title="Azure RAG Chatbot"
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|