Akash9281 commited on
Commit
c4ea1ea
·
verified ·
1 Parent(s): dd26462

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -3,9 +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.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"
@@ -18,7 +17,22 @@ 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,14 +43,9 @@ 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):
@@ -48,9 +57,6 @@ def upload_pdfs(file_list):
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
 
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
7
+ import requests # Added for OpenAI API call
 
8
 
9
  # Azure Configuration
10
  SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
 
17
 
18
  # Azure Clients
19
  search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=AzureKeyCredential(SEARCH_KEY))
20
+
21
+ # Function: Call OpenAI API using Requests
22
+ def call_openai_api(prompt):
23
+ url = f"{OPENAI_ENDPOINT}openai/deployments/gpt-4/completions?api-version=2023-05-15"
24
+ headers = {
25
+ "Content-Type": "application/json",
26
+ "Authorization": f"Bearer {OPENAI_KEY}"
27
+ }
28
+ data = {
29
+ "prompt": prompt,
30
+ "max_tokens": 500,
31
+ "temperature": 0.7
32
+ }
33
+ response = requests.post(url, headers=headers, json=data)
34
+ response_json = response.json()
35
+ return response_json.get("choices", [{}])[0].get("text", "No response from OpenAI API.")
36
 
37
  # Function: Process User Query
38
  def process_query(question, history):
 
43
  )
44
  content = "\n".join([result['content'] for result in search_results])
45
 
46
+ # Call OpenAI API
47
+ prompt = f"Use the following content to answer the question:\n{content}\nQuestion: {question}"
48
+ return call_openai_api(prompt)
 
 
 
 
 
49
 
50
  # Function: Upload PDFs and Reindex
51
  def upload_pdfs(file_list):
 
57
  blob_client = container_client.get_blob_client(blob_name)
58
  blob_client.upload_blob(file, overwrite=True)
59
 
 
 
 
60
  return "PDFs uploaded successfully! Reindexing may take some time."
61
 
62
  # Gradio UI