Spaces:
Sleeping
Sleeping
File size: 2,029 Bytes
2a4f8c1 c587702 2a4f8c1 c587702 2a4f8c1 c587702 2a4f8c1 c587702 2a4f8c1 |
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 |
import gradio as gr
import requests
import os
# Load instructions from local files
def load_instruction(persona):
try:
with open(f"instructions/{persona.lower()}.txt", "r") as file:
return file.read()
except FileNotFoundError:
return ""
# Call Cohere R+ model via API
def call_cohere_api(system_instruction, user_prompt):
headers = {
"Authorization": f"Bearer {os.getenv('COHERE_API_KEY')}",
"Content-Type": "application/json"
}
payload = {
"model": "command-r-plus",
"message": user_prompt,
"preamble": system_instruction,
"max_tokens": 300
}
response = requests.post("https://api.cohere.ai/v1/chat", headers=headers, json=payload)
if response.status_code == 200:
return response.json().get("text", "No response")
else:
return f"Error: {response.status_code} - {response.text}"
# Wrapper for dual assistant responses
def ask_forest_oracle(persona1, persona2, prompt):
instruction1 = load_instruction(persona1)
instruction2 = load_instruction(persona2)
response1 = call_cohere_api(instruction1, prompt)
response2 = call_cohere_api(instruction2, prompt)
return response1.strip(), response2.strip()
# UI Elements
personas = ["Tree", "Crow"]
with gr.Blocks() as demo:
gr.Markdown("## 🌳🪶 More Than Human Voices")
with gr.Row():
persona1 = gr.Dropdown(personas, label="Choose First Persona", value="Tree")
persona2 = gr.Dropdown(personas, label="Choose Second Persona", value="Crow")
user_input = gr.Textbox(label="Your Question to the them", placeholder="e.g., What do you think of humans?", lines=2)
with gr.Row():
output1 = gr.Textbox(label="Response from Persona 1")
output2 = gr.Textbox(label="Response from Persona 2")
ask_button = gr.Button("Ask them")
ask_button.click(fn=ask_forest_oracle, inputs=[persona1, persona2, user_input], outputs=[output1, output2])
if __name__ == "__main__":
demo.launch()
|