Tonic commited on
Commit
aac91b8
·
1 Parent(s): d300b6a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ import typing
3
+ from aiser import RestAiServer, KnowledgeBase, SemanticSearchResult, Agent
4
+ from aiser.models import ChatMessage
5
+ import asyncio
6
+ import gradio as gr
7
+ import requests
8
+ import os
9
+
10
+ # Define environment variables
11
+ API_URL = os.getenv("API_URL", "YOUR_API_URL_PLACEHOLDER")
12
+ API_TOKEN = os.getenv("API_TOKEN", "YOUR_API_TOKEN_PLACEHOLDER")
13
+
14
+ class ChatBot:
15
+ def __init__(self):
16
+ self.history = []
17
+
18
+ def predict(self, input):
19
+ new_user_input = input # User input should be converted into model input format
20
+
21
+ # Prepare payload for API call
22
+ payload = {"question": new_user_input}
23
+
24
+ # Make an external API call
25
+ headers = {"Authorization": API_TOKEN}
26
+ response = requests.post(API_URL, headers=headers, json=payload)
27
+ if response.status_code == 200:
28
+ chat_history_ids = response.json()
29
+ else:
30
+ chat_history_ids = {"response": "Error in API call"}
31
+
32
+ # Process the API response and update history
33
+ self.history.append(chat_history_ids['response'])
34
+ response_text = chat_history_ids['response']
35
+ return response_text
36
+
37
+ bot = ChatBot()
38
+
39
+ title = "👋🏻Welcome to Tonic's EZ Chat🚀"
40
+ 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."
41
+ examples = [["How are you?"]]
42
+
43
+ iface = gr.Interface(
44
+ fn=bot.predict,
45
+ title=title,
46
+ description=description,
47
+ examples=examples,
48
+ inputs="text",
49
+ outputs="text",
50
+ theme="Tonic/indiansummer"
51
+ )
52
+
53
+ iface.launch()
54
+
55
+ class KnowledgeBaseExample(KnowledgeBase):
56
+ def perform_semantic_search(self, query_text: str, desired_number_of_results: int) -> List[SemanticSearchResult]:
57
+ result_example = SemanticSearchResult(
58
+ content="This is an example of a semantic search result",
59
+ score=0.5,
60
+ )
61
+ return [result_example for _ in range(desired_number_of_results)]
62
+
63
+ class AgentExample(Agent):
64
+ async def reply(self, messages: typing.List[ChatMessage]) -> typing.AsyncGenerator[ChatMessage, None]:
65
+ reply_message = "This is an example of a reply from an agent"
66
+ for character in reply_message:
67
+ yield ChatMessage(text_content=character)
68
+ await asyncio.sleep(0.1)
69
+
70
+ if __name__ == '__main__':
71
+ server = RestAiServer(
72
+ agents=[
73
+ AgentExample(
74
+ agent_id='10209b93-2dd0-47a0-8eb2-33fb018a783b' # replace with your agent id
75
+ ),
76
+ ],
77
+ knowledge_bases=[
78
+ KnowledgeBaseExample(
79
+ knowledge_base_id='85bc1c72-b8e0-4042-abcf-8eb2d478f207' # replace with your knowledge base id
80
+ ),
81
+ ],
82
+ port=5000
83
+ )
84
+ server.run()