File size: 2,108 Bytes
2329051
df7050b
e96f6f0
2329051
f787fa1
 
2329051
 
 
 
 
 
67a06c2
 
 
 
 
e96f6f0
 
67a06c2
 
 
 
 
2329051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67a06c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2329051
f787fa1
df7050b
67a06c2
 
 
 
2329051
 
 
 
df7050b
2329051
67a06c2
 
 
df7050b
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
import gradio as gr
from uuid import uuid4 as uuid
from huggingface_hub import HfApi
from typing import Any
from griptape.structures import Agent, Workflow
from griptape.tasks import PromptTask
from griptape.drivers import LocalConversationMemoryDriver
from griptape.memory.structure import ConversationMemory
from griptape.tools import Calculator
from griptape.rules import Rule
import time


# Idea - what if we could make an api call to huggingface and add secret .env variables for each person 
# In this case, it should run no matter what 
# Can add a .env that has session ID and use Redis for conversation memory

repo_id = "kateforsberg/gradio-test"
api = HfApi()
#api.add_space_secret(repo_id=repo_id, key='OPENAI_API_KEY', value='sk-1234')
# Could this create a variable that's unique to each session? 
#api.add_space_variable(repo_id=repo_id, key='SESSION_ID', value=str(uuid()))

sessions = {}

def user(user_message, history):
    history.append([user_message, None])
    return ("", history)

def bot(history):
    response = agent.send_message(history[-1][0])
    history[-1][1] = ""
    for character in response:
        history[-1][1] += character

        time.sleep(0.005)

        yield history

def build_agent(session_id:str):
    return Agent(
            conversation_memory=ConversationMemory(
                driver=LocalConversationMemoryDriver(
                    file_path=f'{session_id}.json'
                )),
            tools=[Calculator()],
            rules=[
                Rule(
                    value = "You are an intelligent agent tasked with answering questions."
                ),
                Rule(
                    value = "All of your responses are less than 5 sentences."
                ) ]
    )



def send_message(message:str, history, request:gr.Request) -> Any:
    if request:
        session_hash = request.session_hash
        agent = build_agent(session_hash)
    response = agent.run(message)
    return response.output.value

demo = gr.ChatInterface(
    fn=send_message,
    )
demo.launch(share=True)

#auth=("griptape","griptaper")