Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,81 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
# Azure OpenAI
|
| 5 |
-
OPENAI_ENDPOINT = "https://
|
| 6 |
-
OPENAI_KEY = "
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 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 |
-
"
|
| 15 |
}
|
| 16 |
-
|
| 17 |
"prompt": prompt,
|
| 18 |
"max_tokens": 500,
|
| 19 |
"temperature": 0.7
|
| 20 |
}
|
| 21 |
-
response = requests.post(
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
return response.json()["choices"][0]["text"].strip()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"
|
| 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 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
inputs=
|
| 63 |
-
outputs=
|
| 64 |
-
title="Azure-Powered RAG Chatbot",
|
| 65 |
-
live=True
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 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()
|
|
|
|
|
|
|
|
|