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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -51
app.py CHANGED
@@ -1,71 +1,81 @@
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 = {
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
- 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()
 
 
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()