Akash9281 commited on
Commit
2df3289
·
verified ·
1 Parent(s): e3e0c8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -62
app.py CHANGED
@@ -1,21 +1,12 @@
1
  import gradio as gr
2
- from azure.search.documents import SearchClient
3
- from azure.search.documents.models import QueryType
4
- from azure.core.credentials import AzureKeyCredential
5
  import requests
6
 
7
- # Azure Configuration
8
- SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
9
- SEARCH_KEY = "Kq2Ww1XBwGCvV4JXTMvWT6qo1O9HprGo74elTSNYHiAzSeDETx4y"
10
- INDEX_NAME = "rag-index-ak"
11
- OPENAI_ENDPOINT = "https://rag-openai-service-ak.openai.azure.com/"
12
- OPENAI_KEY = "B1XyCaz87o456EVD949oODcGC8KTAEQsNLI7Yq5cnYKk41SMY9PtJQQJ99AKACHYHv6XJ3w3AAABACOGAaCZ"
13
  DEPLOYMENT_ID = "gpt-4"
14
 
15
- # Azure Cognitive Search Client
16
- search_client = SearchClient(endpoint=SEARCH_ENDPOINT, index_name=INDEX_NAME, credential=AzureKeyCredential(SEARCH_KEY))
17
-
18
- # Function to call Azure OpenAI API
19
  def call_openai_api(prompt):
20
  url = f"{OPENAI_ENDPOINT}openai/deployments/{DEPLOYMENT_ID}/completions?api-version=2023-05-15"
21
  headers = {
@@ -28,62 +19,53 @@ def call_openai_api(prompt):
28
  "temperature": 0.7
29
  }
30
  response = requests.post(url, headers=headers, json=data)
31
- if response.status_code == 200:
32
- return response.json()["choices"][0]["text"].strip()
33
- return f"Error: {response.status_code} - {response.text}"
34
 
35
- # Function to process user queries
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def process_query(question):
37
- try:
38
- # Search in Azure Cognitive Search
39
- search_results = search_client.search(
40
- search_text=question,
41
- query_type=QueryType.SIMPLE,
42
- top=3
43
- )
44
- # Extract search results content
45
- content = "\n".join([result['content'] for result in search_results])
46
- if not content:
47
- return "No relevant information found in the indexed documents."
48
-
49
- # Use OpenAI to generate an enhanced response
50
- prompt = f"Use the following content to answer the question:\n{content}\nQuestion: {question}"
51
- return call_openai_api(prompt)
52
- except Exception as e:
53
- return f"Error during query processing: {e}"
54
 
55
- # Predefined questions
56
- predefined_questions = [
57
- "What is the purpose of the Master of Engineering Handbook?",
58
- "What are the career opportunities in AI?",
59
- "What are the program requirements for the Master of Engineering?",
60
- "How can I build a career in AI?",
61
- "What does the Master of Engineering handbook say about internships?"
62
- ]
63
 
64
  # Gradio UI
65
  def chatbot_ui(question):
66
- if question:
67
- response = process_query(question)
68
- return response
69
- return "Please select a predefined question or enter a valid query."
 
70
 
71
- # Gradio Interface
72
- with gr.Blocks() as demo:
73
- gr.Markdown("### Azure-Powered RAG Chatbot")
74
- with gr.Row():
75
- with gr.Column():
76
- question_dropdown = gr.Dropdown(
77
- choices=predefined_questions,
78
- label="Select a predefined question",
79
- value=predefined_questions[0]
80
- )
81
- submit_button = gr.Button("Submit")
82
- with gr.Column():
83
- response_output = gr.Textbox(label="Chatbot Response", lines=10)
84
 
85
- submit_button.click(fn=chatbot_ui, inputs=question_dropdown, outputs=response_output)
86
 
87
- # Run the Gradio app
88
  if __name__ == "__main__":
89
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
 
 
 
2
  import requests
3
 
4
+ # Azure OpenAI Configuration
5
+ OPENAI_ENDPOINT = "https://<your-openai-resource-name>.openai.azure.com/"
6
+ OPENAI_KEY = "GGdd0WBghN8iarseTni7x4SxRlINgXrCiQdExooi8qXXwQleIpnjJQQJ99AKACHYHv6XJ3w3AAABACOGHD3K"
 
 
 
7
  DEPLOYMENT_ID = "gpt-4"
8
 
9
+ # Function to Call Azure OpenAI API
 
 
 
10
  def call_openai_api(prompt):
11
  url = f"{OPENAI_ENDPOINT}openai/deployments/{DEPLOYMENT_ID}/completions?api-version=2023-05-15"
12
  headers = {
 
19
  "temperature": 0.7
20
  }
21
  response = requests.post(url, headers=headers, json=data)
 
 
 
22
 
23
+ if response.status_code != 200:
24
+ return f"Error: {response.status_code} - {response.text}"
25
+
26
+ return response.json()["choices"][0]["text"].strip()
27
+
28
+ # Predefined Questions
29
+ def get_predefined_questions():
30
+ return [
31
+ "What are the career opportunities in AI?",
32
+ "What is the scope of AI in engineering?",
33
+ "What are the key highlights of the Master of Engineering Handbook?",
34
+ "How to start a career in AI?",
35
+ ]
36
+
37
+ # Function to Process Query
38
  def process_query(question):
39
+ predefined_responses = {
40
+ "What are the career opportunities in AI?": "AI offers diverse opportunities in data science, machine learning engineering, robotics, and more.",
41
+ "What is the scope of AI in engineering?": "AI in engineering includes predictive maintenance, process optimization, and smart systems.",
42
+ "What are the key highlights of the Master of Engineering Handbook?": "The handbook highlights program structure, course requirements, and career support for MEng students.",
43
+ "How to start a career in AI?": "Starting a career in AI involves learning programming, data science, and machine learning frameworks."
44
+ }
45
+
46
+ if question in predefined_responses:
47
+ return predefined_responses[question]
 
 
 
 
 
 
 
 
48
 
49
+ # Use Azure OpenAI for unlisted questions
50
+ return call_openai_api(question)
 
 
 
 
 
 
51
 
52
  # Gradio UI
53
  def chatbot_ui(question):
54
+ response = process_query(question)
55
+ return response
56
+
57
+ def main():
58
+ predefined_questions = get_predefined_questions()
59
 
60
+ demo = gr.Interface(
61
+ fn=chatbot_ui,
62
+ inputs=gr.Dropdown(choices=predefined_questions, label="Select a predefined question"),
63
+ outputs=gr.Textbox(label="Chatbot Response"),
64
+ title="Azure-Powered RAG Chatbot",
65
+ live=True
66
+ )
 
 
 
 
 
 
67
 
68
+ demo.launch()
69
 
 
70
  if __name__ == "__main__":
71
+ main()