File size: 1,668 Bytes
058ae1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from gradio_client import Client
import json
import os

client = Client("http://127.0.0.1:7860")

def test_chat():
    print(">>> Testing Chat Greeting...")
    res = client.predict(
        message="Hallo",
        history=[],
        short_answers=False,
        api_name="//chat"
    )
    # res is (chatbot_updated, history_updated)
    print("Response received.")
    print(f"Chatbot content: {res[0][-1]['content'][:100]}...")

def test_assistant_first():
    print("\n>>> Testing Assistant-First History (Role Alternation Fix)...")
    welcome_text = "Hello. I am Sage 6.5. I can consult the Oracle for you. Shall I do a reading for today, for a specific date, or do you have a specific topic? What is your name?"
    history = [{"role": "assistant", "content": welcome_text}]
    res = client.predict(
        message="Mein Name ist Julian.",
        history=history,
        short_answers=False,
        api_name="//chat"
    )
    # This should NOT crash and should return a response
    print(f"Response: {res[0][-1]['content'][:100]}...")
    if "TemplateError" in str(res):
        print("!!! TEST FAILED: TemplateError detected.")
    else:
        print("✔ Test Passed: No role alternation crash.")

def test_export():
    print("\n>>> Testing Export functionality...")
    res = client.predict(api_name="/export_chat")
    if res and res.endswith(".json"):
        print(f"✔ Export successful: {res}")
    else:
        print("!!! Export failed.")

if __name__ == "__main__":
    try:
        test_chat()
        test_assistant_first()
        test_name_detection()
        test_export()
    except Exception as e:
        print(f"!!! Error: {e}")