Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,71 @@
|
|
| 1 |
-
import requests
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
-
# Azure OpenAI
|
| 5 |
-
OPENAI_ENDPOINT = "https
|
| 6 |
-
OPENAI_KEY = "
|
| 7 |
-
|
| 8 |
-
SEARCH_KEY = "Kq2Ww1XBwGCvV4JXTMvWT6qo1O9HprGo74elTSNYHiAzSeDETx4y"
|
| 9 |
-
INDEX_NAME = "rag-index-ak"
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 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 |
-
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:
|
| 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 |
-
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Gradio UI
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
chatbot_response = gr.Textbox(label="Chatbot Response")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
chatbot_ui,
|
| 77 |
-
inputs=
|
| 78 |
-
outputs=
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|