Akash9281 commited on
Commit
e1f6d3a
·
verified ·
1 Parent(s): 82bb0c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -60
app.py CHANGED
@@ -1,81 +1,66 @@
1
- import requests
2
  import gradio as gr
 
3
 
4
- # Azure OpenAI and Cognitive Search Service Details
5
- OPENAI_ENDPOINT = "https://rag-openai-service-ak.openai.azure.com/openai/deployments/gpt-4-rag-ak/completions?api-version=2023-05-15"
6
  OPENAI_KEY = "B1XyCaz87o456EVD949oODcGC8KTAEQsNLI7Yq5cnYKk41SMY9PtJQQJ99AKACHYHv6XJ3w3AAABACOGAaCZ"
7
- SEARCH_ENDPOINT = "https://rag-search-service-ak.search.windows.net"
8
- SEARCH_KEY = "Kq2Ww1XBwGCvV4JXTMvWT6qo1O9HprGo74elTSNYHiAzSeDETx4y"
9
- INDEX_NAME = "rag-index-ak"
10
 
11
- def query_openai(prompt):
 
 
12
  headers = {
13
  "Content-Type": "application/json",
14
- "api-key": OPENAI_KEY
15
  }
16
- payload = {
17
  "prompt": prompt,
18
  "max_tokens": 500,
19
  "temperature": 0.7
20
  }
21
- response = requests.post(OPENAI_ENDPOINT, headers=headers, json=payload)
22
- if response.status_code == 401:
23
- return "Error: Unauthorized. Please check your OpenAI API key or endpoint."
24
- elif response.status_code == 429:
25
- return "Error: Too Many Requests. Please try again later."
26
- response.raise_for_status()
27
- return response.json()["choices"][0]["text"].strip()
28
 
29
- def search_documents(query):
30
- headers = {
31
- "Content-Type": "application/json",
32
- "api-key": SEARCH_KEY
33
- }
34
- payload = {
35
- "search": query,
36
- "top": 5
37
- }
38
- response = requests.post(f"{SEARCH_ENDPOINT}/indexes/{INDEX_NAME}/docs/search?api-version=2021-04-30-Preview", headers=headers, json=payload)
39
  if response.status_code != 200:
40
- return "Error: Unable to fetch search results."
41
- results = response.json()["value"]
42
- content = "\n".join([doc["content"] for doc in results])
43
- return content
44
-
45
- def process_query(query):
46
- search_results = search_documents(query)
47
- full_prompt = f"Context: {search_results}\n\nUser Question: {query}\n\nAnswer:"
48
- return query_openai(full_prompt)
49
 
50
- # UI Functionality
51
- def chatbot_ui(predefined_question, user_question):
52
- if predefined_question:
53
- return process_query(predefined_question)
54
- elif user_question:
55
- return process_query(user_question)
56
- else:
57
- return "Please select or type a question to proceed."
58
 
59
  # Predefined Questions
60
- predefined_questions = [
61
- "What are the career opportunities in AI?",
62
- "What is the CPT start date?",
63
- "What is the MEng Handbook about?",
64
- ]
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  # Gradio UI
67
- with gr.Blocks() as demo:
68
- gr.Markdown("# Azure-Powered RAG Chatbot")
69
- with gr.Row():
70
- predefined_dropdown = gr.Dropdown(label="Select a predefined question", choices=predefined_questions)
71
- user_question_input = gr.Textbox(label="Or type your own question")
72
- submit_button = gr.Button("Submit")
73
- chatbot_response = gr.Textbox(label="Chatbot Response")
74
 
75
- submit_button.click(
76
- chatbot_ui,
77
- inputs=[predefined_dropdown, user_question_input],
78
- outputs=chatbot_response
 
 
79
  )
80
 
81
- demo.launch()
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
 
4
+ # Azure OpenAI Configuration
5
+ OPENAI_ENDPOINT = "https://rag-openai-service-ak.openai.azure.com/"
6
  OPENAI_KEY = "B1XyCaz87o456EVD949oODcGC8KTAEQsNLI7Yq5cnYKk41SMY9PtJQQJ99AKACHYHv6XJ3w3AAABACOGAaCZ"
7
+ DEPLOYMENT_ID = "gpt-4-rag-ak"
 
 
8
 
9
+ def call_openai_api(prompt):
10
+ """Call the Azure OpenAI API with the provided prompt."""
11
+ url = f"{OPENAI_ENDPOINT}openai/deployments/{DEPLOYMENT_ID}/completions?api-version=2023-05-15"
12
  headers = {
13
  "Content-Type": "application/json",
14
+ "Authorization": f"Bearer {OPENAI_KEY}"
15
  }
16
+ data = {
17
  "prompt": prompt,
18
  "max_tokens": 500,
19
  "temperature": 0.7
20
  }
 
 
 
 
 
 
 
21
 
22
+ response = requests.post(url, headers=headers, json=data)
 
 
 
 
 
 
 
 
 
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
+ "What is the CPT start date?",
36
+ "What skills are required for a career in AI?",
37
+ "How does the Master of Engineering program support career growth?",
38
+ "What industries use AI extensively?",
39
+ "How can I learn machine learning?",
40
+ "What are the ethical concerns in AI?"
41
+ ]
42
+
43
+ def process_query(question):
44
+ """Process the query and return the response from Azure OpenAI."""
45
+ return call_openai_api(question)
46
 
47
  # Gradio UI
48
+ def chatbot_ui(question):
49
+ response = process_query(question)
50
+ return response
51
+
52
+ def main():
53
+ predefined_questions = get_predefined_questions()
 
54
 
55
+ demo = gr.Interface(
56
+ fn=chatbot_ui,
57
+ inputs=gr.Dropdown(choices=predefined_questions, label="Select a predefined question"),
58
+ outputs=gr.Textbox(label="Chatbot Response"),
59
+ title="Azure-Powered RAG Chatbot",
60
+ live=True
61
  )
62
 
63
+ demo.launch()
64
+
65
+ if __name__ == "__main__":
66
+ main()