Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,80 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from azure.search.documents import SearchClient
|
|
|
|
|
|
|
| 3 |
from azure.core.credentials import AzureKeyCredential
|
| 4 |
|
| 5 |
-
# Azure
|
| 6 |
SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
chatbot = gr.Chatbot(label="Azure RAG Chatbot")
|
| 30 |
-
msg = gr.Textbox(label="Your Question:")
|
| 31 |
-
clear = gr.Button("Clear")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
# Launch App
|
| 47 |
if __name__ == "__main__":
|
| 48 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
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.ai.openai import OpenAIClient
|
| 7 |
from azure.core.credentials import AzureKeyCredential
|
| 8 |
|
| 9 |
+
# Azure Configuration
|
| 10 |
SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
|
| 11 |
+
SEARCH_KEY = "Kq2Ww1XBwGCvV4JXTMvWT6qo1O9HprGo74elTSNYHiAzSeDETx4y"
|
| 12 |
+
INDEX_NAME = "rag-index-ak"
|
| 13 |
+
OPENAI_ENDPOINT = "https://rag-openai-service-ak.openai.azure.com/"
|
| 14 |
+
OPENAI_KEY = "B1XyCaz87o456EVD949oODcGC8KTAEQsNLI7Yq5cnYKk41SMY9PtJQQJ99AKACHYHv6XJ3w3AAABACOGAaCZ"
|
| 15 |
+
BLOB_CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=blobstorageak;AccountKey=r5AuAs93I3lLQpLq/Vhp43RkimOxIf3YQ4urlISTxTk07VUcZLzyH+7srlM0NAXjC0mnoHcjyMjS+AStuOv1Gw==;EndpointSuffix=core.windows.net"
|
| 16 |
+
BLOB_CONTAINER_NAME = "uploaded-files"
|
| 17 |
|
| 18 |
+
# Azure Clients
|
| 19 |
+
search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=AzureKeyCredential(SEARCH_KEY))
|
| 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 |
+
# Search the Cognitive Search index
|
| 26 |
+
search_results = search_client.search(
|
| 27 |
+
search_text=question,
|
| 28 |
+
query_type=QueryType.SIMPLE,
|
| 29 |
+
top=3
|
| 30 |
+
)
|
| 31 |
+
# Extract content from search results
|
| 32 |
+
content = "\n".join([result["content"] for result in search_results])
|
| 33 |
|
| 34 |
+
# Use OpenAI GPT to answer the question
|
| 35 |
+
openai_response = openai_client.completions.create(
|
| 36 |
+
deployment_id="gpt-4",
|
| 37 |
+
prompt=f"Use the following content to answer the question:\n{content}\nQuestion: {question}",
|
| 38 |
+
max_tokens=500,
|
| 39 |
+
temperature=0.7
|
| 40 |
+
)
|
| 41 |
+
return openai_response.choices[0].text.strip()
|
| 42 |
+
|
| 43 |
+
# Function: Upload PDFs and Trigger Reindexing
|
| 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, overwrite=True)
|
| 52 |
|
| 53 |
+
# Note: Trigger reindexing via Azure portal or APIs (not implemented here)
|
| 54 |
+
return "PDFs uploaded successfully! The index will update shortly."
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Function: Handle User Interactions
|
| 57 |
+
def chatbot_ui(question, files=None):
|
| 58 |
+
if files:
|
| 59 |
+
# Upload files and notify the user
|
| 60 |
+
upload_response = upload_pdfs(files)
|
| 61 |
+
return upload_response
|
| 62 |
|
| 63 |
+
# Process the question and return the response
|
| 64 |
+
response = process_query(question)
|
| 65 |
+
return response
|
| 66 |
|
| 67 |
+
# Gradio Interface
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=chatbot_ui,
|
| 70 |
+
inputs=[
|
| 71 |
+
gr.Textbox(label="Ask a question"),
|
| 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__":
|
| 80 |
demo.launch()
|