Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,22 @@ 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 |
-
import
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Azure Configuration
|
| 9 |
SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
|
| 10 |
SEARCH_KEY = "Kq2Ww1XBwGCvV4JXTMvWT6qo1O9HprGo74elTSNYHiAzSeDETx4y"
|
| 11 |
INDEX_NAME = "rag-index-ak"
|
| 12 |
-
|
|
|
|
| 13 |
BLOB_CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=blobstorageak;AccountKey=r5AuAs93I3lLQpLq/Vhp43RkimOxIf3YQ4urlISTxTk07VUcZLzyH+7srlM0NAXjC0mnoHcjyMjS+AStuOv1Gw==;EndpointSuffix=core.windows.net"
|
| 14 |
BLOB_CONTAINER_NAME = "dataragak"
|
| 15 |
|
| 16 |
-
# OpenAI Setup
|
| 17 |
-
openai.api_key = OPENAI_API_KEY
|
| 18 |
-
|
| 19 |
# Azure Clients
|
| 20 |
-
search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=SEARCH_KEY)
|
|
|
|
| 21 |
|
| 22 |
# Function: Process User Query
|
| 23 |
def process_query(question, history):
|
|
@@ -28,56 +29,52 @@ def process_query(question, history):
|
|
| 28 |
)
|
| 29 |
content = "\n".join([result['content'] for result in search_results])
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
prompt=f"Use the following content to answer the question:\n{content}\nQuestion: {question}",
|
| 34 |
max_tokens=500,
|
| 35 |
temperature=0.7
|
| 36 |
)
|
| 37 |
|
| 38 |
-
return
|
| 39 |
|
| 40 |
# Function: Upload PDFs and Reindex
|
| 41 |
def upload_pdfs(file_list):
|
| 42 |
blob_service_client = BlobServiceClient.from_connection_string(BLOB_CONNECTION_STRING)
|
| 43 |
container_client = blob_service_client.get_container_client(BLOB_CONTAINER_NAME)
|
| 44 |
|
| 45 |
-
if not container_client.exists():
|
| 46 |
-
container_client.create_container()
|
| 47 |
-
|
| 48 |
for file in file_list:
|
| 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 |
-
#
|
|
|
|
|
|
|
| 54 |
return "PDFs uploaded successfully! Reindexing may take some time."
|
| 55 |
|
| 56 |
# Gradio UI
|
| 57 |
def chatbot_ui(question, files):
|
| 58 |
-
progress_message = ""
|
| 59 |
if files:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
return progress_message, response
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
gr.Markdown("# Azure-Powered RAG Chatbot")
|
| 67 |
-
with gr.Row():
|
| 68 |
-
with gr.Column():
|
| 69 |
-
question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
|
| 70 |
-
pdf_upload = gr.File(label="Upload PDFs (optional)", file_types=[".pdf"], file_count="multiple")
|
| 71 |
-
submit_button = gr.Button("Submit")
|
| 72 |
-
with gr.Column():
|
| 73 |
-
progress_output = gr.Textbox(label="Progress", placeholder="Progress messages will appear here...")
|
| 74 |
-
response_output = gr.Textbox(label="Chatbot Response", placeholder="Chatbot answers will appear here...")
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
demo.launch()
|
|
|
|
| 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.core.credentials import AzureKeyCredential # Correct credential import
|
| 7 |
+
from azure.ai.openai import OpenAIClient
|
| 8 |
+
from azure.identity import DefaultAzureCredential
|
| 9 |
|
| 10 |
# Azure Configuration
|
| 11 |
SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
|
| 12 |
SEARCH_KEY = "Kq2Ww1XBwGCvV4JXTMvWT6qo1O9HprGo74elTSNYHiAzSeDETx4y"
|
| 13 |
INDEX_NAME = "rag-index-ak"
|
| 14 |
+
OPENAI_ENDPOINT = "https://rag-openai-service-ak.openai.azure.com/"
|
| 15 |
+
OPENAI_KEY = "B1XyCaz87o456EVD949oODcGC8KTAEQsNLI7Yq5cnYKk41SMY9PtJQQJ99AKACHYHv6XJ3w3AAABACOGAaCZ"
|
| 16 |
BLOB_CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=blobstorageak;AccountKey=r5AuAs93I3lLQpLq/Vhp43RkimOxIf3YQ4urlISTxTk07VUcZLzyH+7srlM0NAXjC0mnoHcjyMjS+AStuOv1Gw==;EndpointSuffix=core.windows.net"
|
| 17 |
BLOB_CONTAINER_NAME = "dataragak"
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Azure Clients
|
| 20 |
+
search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=AzureKeyCredential(SEARCH_KEY))
|
| 21 |
+
openai_client = OpenAIClient(credential=DefaultAzureCredential(), endpoint=OPENAI_ENDPOINT)
|
| 22 |
|
| 23 |
# Function: Process User Query
|
| 24 |
def process_query(question, history):
|
|
|
|
| 29 |
)
|
| 30 |
content = "\n".join([result['content'] for result in search_results])
|
| 31 |
|
| 32 |
+
openai_response = openai_client.completions.create(
|
| 33 |
+
deployment_id="gpt-4",
|
| 34 |
prompt=f"Use the following content to answer the question:\n{content}\nQuestion: {question}",
|
| 35 |
max_tokens=500,
|
| 36 |
temperature=0.7
|
| 37 |
)
|
| 38 |
|
| 39 |
+
return openai_response.choices[0].text.strip()
|
| 40 |
|
| 41 |
# Function: Upload PDFs and Reindex
|
| 42 |
def upload_pdfs(file_list):
|
| 43 |
blob_service_client = BlobServiceClient.from_connection_string(BLOB_CONNECTION_STRING)
|
| 44 |
container_client = blob_service_client.get_container_client(BLOB_CONTAINER_NAME)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
for file in file_list:
|
| 47 |
blob_name = os.path.basename(file.name)
|
| 48 |
blob_client = container_client.get_blob_client(blob_name)
|
| 49 |
blob_client.upload_blob(file, overwrite=True)
|
| 50 |
|
| 51 |
+
# Trigger reindexing (update indexer in Azure Cognitive Search)
|
| 52 |
+
# Note: Use Azure SDK or REST API to trigger the indexer.
|
| 53 |
+
|
| 54 |
return "PDFs uploaded successfully! Reindexing may take some time."
|
| 55 |
|
| 56 |
# Gradio UI
|
| 57 |
def chatbot_ui(question, files):
|
|
|
|
| 58 |
if files:
|
| 59 |
+
upload_response = upload_pdfs(files)
|
| 60 |
+
return upload_response, ""
|
|
|
|
| 61 |
|
| 62 |
+
response = process_query(question, [])
|
| 63 |
+
return "", response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Gradio App
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
chatbot_ui,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.Textbox(label="Ask a Question", placeholder="Type your question here..."),
|
| 70 |
+
gr.File(label="Upload PDFs (optional)", file_types=[".pdf"], file_count="multiple")
|
| 71 |
+
],
|
| 72 |
+
outputs=[
|
| 73 |
+
gr.Textbox(label="Progress"),
|
| 74 |
+
gr.Textbox(label="Chatbot Response")
|
| 75 |
+
],
|
| 76 |
+
title="Azure-Powered RAG Chatbot"
|
| 77 |
+
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
demo.launch()
|