File size: 2,322 Bytes
c20f7c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import ssl
from openai import OpenAI
import time

# SSL configuration
try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

# OpenAI client setup
client = OpenAI(
    base_url='https://api.openai-proxy.org/v1',
    api_key='sk-Nxf8HmLpfIMhCd83n3TOr00TR57uBZ0jMbAgGCOzppXvlsx1',
)

# Retry logic for OpenAI API call
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  # Increase timeout
            )
            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."

# Chatbot response function
def chatbot_response(message, history):
    # Prepare the conversation history for the API
    messages = [{"role": "system", "content": "You are a dynamic study resoruce database named Arcana. Your goal is to help students study and excel 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})

    # Get response from OpenAI API with retry logic
    response = openai_api_call(messages)
    return response

# Create the Gradio interface
iface = 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",
    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",
)

# Launch the interface
iface.launch(share=True)