|
import gradio as gr |
|
import ssl |
|
from openai import OpenAI |
|
import time |
|
import os |
|
import shutil |
|
|
|
|
|
try: |
|
_create_unverified_https_context = ssl._create_unverified_context |
|
except AttributeError: |
|
pass |
|
else: |
|
ssl._create_default_https_context = _create_unverified_https_context |
|
|
|
|
|
client = OpenAI( |
|
base_url='https://api.openai-proxy.org/v1', |
|
api_key='sk-Nxf8HmLpfIMhCd83n3TOr00TR57uBZ0jMbAgGCOzppXvlsx1', |
|
) |
|
|
|
|
|
def openai_api_call(messages, retries=3, delay=5): |
|
for attempt in range(retries): |
|
try: |
|
completion = client.chat.completions.create( |
|
model="gpt-3.5-turbo", |
|
messages=messages, |
|
timeout=10 |
|
) |
|
return completion.choices[0].message.content |
|
except Exception as e: |
|
print(f"Attempt {attempt + 1} failed: {e}") |
|
time.sleep(delay) |
|
return "Sorry, I am having trouble connecting to the server. Please try again later." |
|
|
|
|
|
def chatbot_response(message, history): |
|
|
|
messages = [{"role": "system", "content": "You are a dynamic study resource database named Arcana. Your goal is to help students study and excel in their exams."}] |
|
for human, assistant in history: |
|
messages.append({"role": "user", "content": human}) |
|
messages.append({"role": "assistant", "content": assistant}) |
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
response = openai_api_call(messages) |
|
return response |
|
|
|
def upload_file(file): |
|
foldername = 'cache' |
|
if not os.path.exists(foldername): |
|
os.mkdir(foldername) |
|
file_path = os.path.join(foldername, os.path.basename(file.name)) |
|
shutil.copy(file.name, file_path) |
|
return list_uploaded_files() |
|
|
|
def list_uploaded_files(): |
|
foldername = 'cache' |
|
if not os.path.exists(foldername): |
|
return [] |
|
files = os.listdir(foldername) |
|
return [[file] for file in files] |
|
|
|
def refresh_files(): |
|
return list_uploaded_files() |
|
|
|
|
|
chatbot_interface = gr.ChatInterface( |
|
chatbot_response, |
|
chatbot=gr.Chatbot(height=300), |
|
textbox=gr.Textbox(placeholder="Type your message here...", container=False, scale=7), |
|
title="Review With Arcana", |
|
description="ArcanaUI v0.7 - Chatbot", |
|
theme="soft", |
|
examples=[ |
|
"What is Hydrogen Bonding?", |
|
"Tell me the difference between impulse and force.", |
|
"Tell me a joke that Calculus students will know.", |
|
"How should I review for the AP Biology Exam?" |
|
], |
|
cache_examples=False, |
|
retry_btn=None, |
|
undo_btn="Delete Previous", |
|
clear_btn="Clear" |
|
) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# ArcanaUI v0.7") |
|
with gr.Tabs(): |
|
with gr.TabItem("Welcome Page"): |
|
gr.Markdown(""" |
|
# Welcome to ArcanaUI v0.7 by the Indexademics Team |
|
Program Base Powered by StandardCAS™ |
|
## Introduction |
|
Welcome to Arcana, your dynamic study resource database! Our goal is to help students like you excel in your exams by providing quick and accurate answers to your study questions. |
|
|
|
## How to Use |
|
- Navigate to the 'Chatbot' tab to ask your study-related questions. |
|
- Type your question into the textbox and press Enter. |
|
- The chatbot will respond with helpful information. |
|
- Use the 'Delete Previous' button to remove the last interaction or 'Clear' to reset the chat. |
|
|
|
## Works Cited |
|
Below is a sample citation in BibTeX format: |
|
``` |
|
@article{Fan2023CELSIA, |
|
title={CELSIA-Nylon}, |
|
author={Chengjui Fan}, |
|
journal={Conf-MLA 2023}, |
|
year={2023}, |
|
volume={NAN}, |
|
number={NAN}, |
|
pages={NAN}, |
|
publisher={Conf-MLA} |
|
} |
|
|
|
@misc{Indexademics, |
|
title={indexademics Chatbot}, |
|
author={NAN}, |
|
journal={SHSID}, |
|
year={2024}, |
|
volume={NAN}, |
|
number={NAN}, |
|
pages={NAN}, |
|
publisher={Peer Advisor(PA) SHSID} |
|
} |
|
``` |
|
""") |
|
|
|
with gr.TabItem("Chatbot"): |
|
chatbot_interface.render() |
|
|
|
|
|
with gr.TabItem('Upload'): |
|
gr.Markdown('# Upload and View Files') |
|
refresh_button = gr.Button('Refresh') |
|
upload_button = gr.UploadButton('Upload File') |
|
uploaded_files_list = gr.DataFrame(headers=["Uploaded Files"]) |
|
refresh_button.click(fn=refresh_files, outputs=uploaded_files_list) |
|
upload_button.upload(upload_file, inputs=upload_button, outputs=uploaded_files_list) |
|
gr.Row([upload_button, refresh_button, uploaded_files_list]) |
|
|
|
|
|
demo.launch(share=True) |
|
|