TrancheVest's picture
Fix error handling and improve crypto data retrieval
a2df663
Raw History Blame Contribute Delete
2.05 kB
import gradio as gr
import time
from utils import (
get_question,
get_answer,
evaluate_expertise,
get_crypto_data,
)
def run_chat(crypto, data_span):
# Validate inputs
valid_cryptos = ["btc", "eth", "virtual", "bnb"]
valid_spans = ["24h", "7d", "14d", "30d", "60d"]
if crypto not in valid_cryptos or data_span not in valid_spans:
yield [("๐Ÿค–", "โŒ Invalid crypto or data span selection")], []
return
crypto_data = get_crypto_data(crypto, data_span)
chat_history = []
previous_qa = ""
qa_history = []
for _ in range(5):
# --- QUESTION ---
question = get_question(previous_qa)
# append as a right-only message
chat_history.append(("", question.content))
yield chat_history, [] # show question
time.sleep(1)
# --- ANSWER ---
answer = get_answer(question.content, previous_qa, crypto_data)
# append as a left-only message
chat_history.append((answer.content, ""))
yield chat_history, [] # show answer
# track for final evaluation
qa_history.append({"question": question.content, "answer": answer.content})
previous_qa += f"\nQ: {question.content}\nA: {answer.content}\n"
time.sleep(1)
# --- FINAL EVALUATION ---
evaluation = evaluate_expertise(qa_history)
chat_history.append(("๐Ÿ“Š Evaluation:", evaluation.content))
yield chat_history, chat_history
with gr.Blocks() as demo:
gr.Markdown("## ๐Ÿง  TranchVest AI - Crypto Behavior Analysis")
with gr.Row():
crypto_dropdown = gr.Dropdown(
["btc", "eth", "virtual", "bnb"], label="Select Crypto"
)
span_dropdown = gr.Dropdown(
["24h", "7d", "14d", "30d", "60d"], label="Select Data Span"
)
start_btn = gr.Button("Start Chat")
chatbot = gr.Chatbot(height=600)
state = gr.State([])
start_btn.click(
run_chat,
[crypto_dropdown, span_dropdown],
[chatbot, state],
)
demo.launch()