Akash9281 commited on
Commit
7f4c3f3
·
verified ·
1 Parent(s): bbcf00a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -32
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 Search and OpenAI Configuration
6
  SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
7
- SEARCH_API_KEY = "Kq2Ww1XBwGCvV4JXTMvWT6qo1O9HprGo74elTSNYHiAzSeDETx4y"
8
- SEARCH_INDEX_NAME = "rag-index-ak"
 
 
 
 
9
 
10
- client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=SEARCH_INDEX_NAME, credential=AzureKeyCredential(SEARCH_API_KEY))
 
 
 
11
 
12
- def query_rag(user_query):
13
- # Azure Search Query
14
- results = client.search(search_text=user_query, top=5)
15
- sources = "\n".join([doc["content"] for doc in results])
 
 
 
 
 
 
16
 
17
- # Simulated RAG Output
18
- response = f"Relevant sources:\n{sources}\n\nAnswer:\nSimulated AI-generated answer based on sources."
19
- return response
 
 
 
 
 
 
 
 
 
20
 
21
- # Define Gradio App
22
- def chat_function(message, history):
23
- response = query_rag(message)
24
- history.append((message, response))
25
- return history, history
26
 
27
- # Gradio Chat Interface
28
- with gr.Blocks() as demo:
29
- chatbot = gr.Chatbot(label="Azure RAG Chatbot")
30
- msg = gr.Textbox(label="Your Question:")
31
- clear = gr.Button("Clear")
32
 
33
- examples = [
34
- ["What is the OPT start date?"],
35
- ["What is the deadline for application submission?"],
36
- ["What is the MEng program duration?"],
37
- ["What are the available concentrations in MEng?"],
38
- ]
39
 
40
- def clear_chat():
41
- return [], []
 
42
 
43
- msg.submit(chat_function, [msg, chatbot], [chatbot, chatbot])
44
- clear.click(clear_chat, None, [chatbot])
 
 
 
 
 
 
 
 
 
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()