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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -45
app.py CHANGED
@@ -2,70 +2,73 @@ 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()
 
 
2
  import requests
3
 
4
  # Azure OpenAI Configuration
 
5
  OPENAI_KEY = "GGdd0WBghN8iarseTni7x4SxRlINgXrCiQdExooi8qXXwQleIpnjJQQJ99AKACHYHv6XJ3w3AAABACOGHD3K"
6
+ OPENAI_ENDPOINT = "https://<your-openai-resource-name>.openai.azure.com/openai/deployments/gpt-4/completions?api-version=2023-05-15"
7
 
8
+ # Predefined Questions
9
+ PREDEFINED_QUESTIONS = [
10
+ "What are the career opportunities in AI?",
11
+ "What does the MEng handbook say about course credits?",
12
+ "What are the top skills required for AI careers?",
13
+ "How do I start building a career in AI?",
14
+ "What is the structure of the MEng program?"
15
+ ]
16
+
17
+ # Function to query Azure OpenAI
18
+ def query_openai(prompt):
19
  headers = {
20
  "Content-Type": "application/json",
21
  "Authorization": f"Bearer {OPENAI_KEY}"
22
  }
23
+ payload = {
24
  "prompt": prompt,
25
  "max_tokens": 500,
26
  "temperature": 0.7
27
  }
28
+ response = requests.post(OPENAI_ENDPOINT, headers=headers, json=payload)
29
 
30
+ if response.status_code == 200:
31
+ return response.json()["choices"][0]["text"].strip()
32
+ else:
33
  return f"Error: {response.status_code} - {response.text}"
34
 
35
+ # Main function to handle user input
36
+ def chatbot_ui(predefined_question, custom_question):
37
+ if custom_question:
38
+ question = custom_question
39
+ else:
40
+ question = predefined_question
41
 
42
+ prompt = f"Answer the following question based on the uploaded documents: {question}"
43
+ response = query_openai(prompt)
44
+ return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ # Gradio UI
47
+ def create_ui():
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("## Azure-Powered RAG Chatbot")
50
 
51
+ with gr.Row():
52
+ predefined_question = gr.Dropdown(
53
+ choices=PREDEFINED_QUESTIONS,
54
+ label="Select a predefined question",
55
+ value=PREDEFINED_QUESTIONS[0]
56
+ )
57
+ custom_question = gr.Textbox(label="Or type your own question", placeholder="Type your question here...")
58
 
59
+ with gr.Row():
60
+ submit_button = gr.Button("Submit")
 
 
61
 
62
+ chatbot_response = gr.Textbox(label="Chatbot Response")
 
63
 
64
+ submit_button.click(
65
+ chatbot_ui,
66
+ inputs=[predefined_question, custom_question],
67
+ outputs=chatbot_response
68
+ )
 
 
69
 
70
+ return demo
71
 
72
  if __name__ == "__main__":
73
+ ui = create_ui()
74
+ ui.launch()