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")