File size: 2,867 Bytes
aac91b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import List
import typing
from aiser import RestAiServer, KnowledgeBase, SemanticSearchResult, Agent
from aiser.models import ChatMessage
import asyncio
import gradio as gr
import requests
import os

# Define environment variables
API_URL = os.getenv("API_URL", "YOUR_API_URL_PLACEHOLDER")
API_TOKEN = os.getenv("API_TOKEN", "YOUR_API_TOKEN_PLACEHOLDER")

class ChatBot:
    def __init__(self):
        self.history = []

    def predict(self, input):
        new_user_input = input  # User input should be converted into model input format

        # Prepare payload for API call
        payload = {"question": new_user_input}

        # Make an external API call
        headers = {"Authorization": API_TOKEN}
        response = requests.post(API_URL, headers=headers, json=payload)
        if response.status_code == 200:
            chat_history_ids = response.json()
        else:
            chat_history_ids = {"response": "Error in API call"}

        # Process the API response and update history
        self.history.append(chat_history_ids['response'])
        response_text = chat_history_ids['response']
        return response_text

bot = ChatBot()

title = "👋🏻Welcome to Tonic's EZ Chat🚀"
description = "You can use this Space to test out the current model (DialoGPT-medium) or duplicate this Space and use it for any other model on 🤗HuggingFace. Join me on [Discord](https://discord.gg/fpEPNZGsbt) to build together."
examples = [["How are you?"]]

iface = gr.Interface(
    fn=bot.predict,
    title=title,
    description=description,
    examples=examples,
    inputs="text",
    outputs="text",
    theme="Tonic/indiansummer"
)

iface.launch()

class KnowledgeBaseExample(KnowledgeBase):
    def perform_semantic_search(self, query_text: str, desired_number_of_results: int) -> List[SemanticSearchResult]:
        result_example = SemanticSearchResult(
            content="This is an example of a semantic search result",
            score=0.5,
        )
        return [result_example for _ in range(desired_number_of_results)]

class AgentExample(Agent):
    async def reply(self, messages: typing.List[ChatMessage]) -> typing.AsyncGenerator[ChatMessage, None]:
        reply_message = "This is an example of a reply from an agent"
        for character in reply_message:
            yield ChatMessage(text_content=character)
            await asyncio.sleep(0.1)

if __name__ == '__main__':
    server = RestAiServer(
        agents=[
            AgentExample(
                agent_id='10209b93-2dd0-47a0-8eb2-33fb018a783b'  # replace with your agent id
            ),
        ],
        knowledge_bases=[
            KnowledgeBaseExample(
                knowledge_base_id='85bc1c72-b8e0-4042-abcf-8eb2d478f207'  # replace with your knowledge base id
            ),
        ],
        port=5000
    )
    server.run()