Spaces:
Runtime error
Runtime error
redo as simple blocks app with no export yet
Browse files
app.py
CHANGED
|
@@ -2,17 +2,11 @@
|
|
| 2 |
|
| 3 |
# openai: used to communicate with the OpenAI API for generating model responses.
|
| 4 |
# gradio: provides a simple way to create user interfaces for models.
|
| 5 |
-
# json: allows us to work with JSON data.
|
| 6 |
-
# csv: enables working with CSV files.
|
| 7 |
-
# fpdf: a library to generate PDF files.
|
| 8 |
|
| 9 |
-
#! pip install -q openai gradio
|
| 10 |
|
| 11 |
import openai
|
| 12 |
import gradio as gr
|
| 13 |
-
import json
|
| 14 |
-
import csv
|
| 15 |
-
from fpdf import FPDF
|
| 16 |
|
| 17 |
# Initializing system message which provides initial context and instructions for the model.
|
| 18 |
sys_message = """You are a helpful and friendly coach helping a graduate student reflect on their recent class experience in Advanced Corporate Valuation at Vanderbilt's Owen Graduate School of Management.
|
|
@@ -43,7 +37,6 @@ Wrap up the conversation by praising reflective thinking. Let the student know w
|
|
| 43 |
their reflections are especially thoughtful or demonstrate progress. Let the student know if their
|
| 44 |
reflections reveal a change or growth in thinking.
|
| 45 |
"""
|
| 46 |
-
des = "This chatbot will help you write your final reflection for Advanced Corp Val. Start by saying Hi!"
|
| 47 |
|
| 48 |
# Function Definitions
|
| 49 |
|
|
@@ -53,51 +46,6 @@ des = "This chatbot will help you write your final reflection for Advanced Corp
|
|
| 53 |
# It manages the conversational context/history and ensures that the
|
| 54 |
# conversation flows naturally.
|
| 55 |
|
| 56 |
-
# 2. combined_function:
|
| 57 |
-
# This function handles the logic for downloading the chat history in different formats.
|
| 58 |
-
# Based on the user's choice (JSON, CSV, PDF), it calls the appropriate function
|
| 59 |
-
# to convert the chat history and returns a download link.
|
| 60 |
-
|
| 61 |
-
# 3. save_to_json, save_to_csv, save_to_pdf:
|
| 62 |
-
# These helper functions take the chat history as input and save it to the
|
| 63 |
-
# respective file formats.
|
| 64 |
-
|
| 65 |
-
# Let's integrate the extracted function definitions into our documented content.
|
| 66 |
-
def convert_chat_history(format):
|
| 67 |
-
if format == "json":
|
| 68 |
-
file_path = "chat_history.json"
|
| 69 |
-
with open(file_path, 'w') as file:
|
| 70 |
-
json.dump(chat_log, file)
|
| 71 |
-
elif format == "csv":
|
| 72 |
-
file_path = "chat_history.csv"
|
| 73 |
-
with open(file_path, 'w', newline='') as file:
|
| 74 |
-
writer = csv.DictWriter(file, fieldnames=["role", "content"])
|
| 75 |
-
writer.writeheader()
|
| 76 |
-
for row in chat_log:
|
| 77 |
-
writer.writerow(row)
|
| 78 |
-
elif format == "pdf":
|
| 79 |
-
file_path = "chat_history.pdf"
|
| 80 |
-
pdf = FPDF()
|
| 81 |
-
pdf.add_page()
|
| 82 |
-
pdf.set_font("Arial", size=12)
|
| 83 |
-
for entry in chat_log:
|
| 84 |
-
pdf.cell(200, 10, txt="{}: {}".format(entry["role"].title(), entry["content"]), ln=True)
|
| 85 |
-
pdf.output(file_path)
|
| 86 |
-
else:
|
| 87 |
-
return None
|
| 88 |
-
return file_path
|
| 89 |
-
|
| 90 |
-
def download_json():
|
| 91 |
-
convert_chat_history("json")
|
| 92 |
-
return "Downloaded JSON"
|
| 93 |
-
|
| 94 |
-
def download_csv():
|
| 95 |
-
convert_chat_history("csv")
|
| 96 |
-
return "Downloaded CSV"
|
| 97 |
-
|
| 98 |
-
def download_pdf():
|
| 99 |
-
convert_chat_history("pdf")
|
| 100 |
-
return "Downloaded PDF"
|
| 101 |
|
| 102 |
def CustomChatGPT(message, history):
|
| 103 |
history_openai_format = [{"role": "system", "content": sys_message}]
|
|
@@ -119,58 +67,24 @@ def CustomChatGPT(message, history):
|
|
| 119 |
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
| 120 |
yield partial_message
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
fn=combined_function,
|
| 144 |
-
inputs=[gr.Radio(choices = ["json", "csv", "pdf"], label="Download Options")],
|
| 145 |
-
outputs=gr.Textbox(label = "Download status", placeholder = "Downloading..."),
|
| 146 |
-
live=True,
|
| 147 |
-
layout="vertical",
|
| 148 |
-
theme="huggingface",
|
| 149 |
-
server_name="0.0.0.0",
|
| 150 |
-
server_port=7860,
|
| 151 |
-
allow_flagging=False
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
# Gradio Interface Creation
|
| 155 |
-
|
| 156 |
-
# Two separate Gradio interfaces are created:
|
| 157 |
-
|
| 158 |
-
# 1. chat_ui:
|
| 159 |
-
# An interface for the chatbot, where users can interact with the AI model
|
| 160 |
-
# in a conversational format. It uses the CustomChatGPT function to process
|
| 161 |
-
# user inputs and generate model responses.
|
| 162 |
-
|
| 163 |
-
# 2. download_ui:
|
| 164 |
-
# An interface that allows users to download the chat history in different
|
| 165 |
-
# formats (JSON, CSV, PDF). The combined_function manages the logic for
|
| 166 |
-
# this interface.
|
| 167 |
-
|
| 168 |
-
# The main execution section of the code initializes these interfaces and
|
| 169 |
-
# launches them in parallel, allowing users to switch between the chatbot
|
| 170 |
-
# and download interfaces.
|
| 171 |
-
|
| 172 |
-
demo = gr.Parallel(chat_ui, download_ui)
|
| 173 |
-
#demo = gr.Parallel(chat_ui)
|
| 174 |
-
|
| 175 |
-
if __name__ == "__main__":
|
| 176 |
-
demo.launch()
|
|
|
|
| 2 |
|
| 3 |
# openai: used to communicate with the OpenAI API for generating model responses.
|
| 4 |
# gradio: provides a simple way to create user interfaces for models.
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
#! pip install -q openai gradio
|
| 7 |
|
| 8 |
import openai
|
| 9 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Initializing system message which provides initial context and instructions for the model.
|
| 12 |
sys_message = """You are a helpful and friendly coach helping a graduate student reflect on their recent class experience in Advanced Corporate Valuation at Vanderbilt's Owen Graduate School of Management.
|
|
|
|
| 37 |
their reflections are especially thoughtful or demonstrate progress. Let the student know if their
|
| 38 |
reflections reveal a change or growth in thinking.
|
| 39 |
"""
|
|
|
|
| 40 |
|
| 41 |
# Function Definitions
|
| 42 |
|
|
|
|
| 46 |
# It manages the conversational context/history and ensures that the
|
| 47 |
# conversation flows naturally.
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def CustomChatGPT(message, history):
|
| 51 |
history_openai_format = [{"role": "system", "content": sys_message}]
|
|
|
|
| 67 |
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
| 68 |
yield partial_message
|
| 69 |
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("# Advanced Corp Val AI Coach")
|
| 75 |
+
gr.Markdown("## I am your Advanced Corp Val AI Coach. I'm here to help you with your final reflection on this course. Start by saying hi!")
|
| 76 |
+
|
| 77 |
+
# Redesigned chatbot using Blocks (reference: https://www.geeksforgeeks.org/create-a-chatbot-with-openai-and-gradio-in-python/)
|
| 78 |
+
with gr.Blocks():
|
| 79 |
+
chatbot = gr.Chatbot()
|
| 80 |
+
with gr.Row(equal_height = True):
|
| 81 |
+
message = gr.Textbox(placeholder="Type your questioins here!")
|
| 82 |
+
state = gr.State()
|
| 83 |
+
submit = gr.Button("Send message")
|
| 84 |
+
clear = gr.ClearButton([message, chatbot])
|
| 85 |
+
submit.click(message_and_history,
|
| 86 |
+
inputs=[message, state],
|
| 87 |
+
outputs=[chatbot, state])
|
| 88 |
+
|
| 89 |
+
demo.queue()
|
| 90 |
+
demo.launch(debug = True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|