neovalle commited on
Commit
ae07547
Β·
verified Β·
1 Parent(s): 9589c61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -40
app.py CHANGED
@@ -58,7 +58,7 @@ counternarratives = load_counternarratives()
58
  # Generate Random Myth or Fact and trigger persona response
59
  def get_random_myth_or_fact():
60
  if counternarratives.empty:
61
- return "No myths or facts available.", "Tree", "Fact-Checker 🧠", "Error: No Data Found.", "Error: No Data Found."
62
 
63
  # πŸ”„ Randomly select a row from the dataframe
64
  row = counternarratives.sample(1).iloc[0]
@@ -76,55 +76,33 @@ def get_random_myth_or_fact():
76
  else:
77
  fact_check_response = f"βœ… **FACT**\n\nIndeed, {row['fact']}"
78
 
79
- # Return the myth/fact, set personas, and fill the responses
80
- return myth_or_fact, persona, "Fact-Checker 🧠", persona_response, fact_check_response
81
 
82
  # Dynamically load persona names from instructions folder
83
  personas = [os.path.splitext(f)[0].capitalize() for f in os.listdir("instructions") if f.endswith(".txt")]
84
 
85
  # Gradio Interface
86
  with gr.Blocks() as demo:
87
- # Header with Image
88
  with gr.Row():
89
- with gr.Column(scale=0.15):
90
- gr.Image(value="data/WildVoices.png", label="Wild Voices", show_label=False)
91
- with gr.Column():
92
- gr.Markdown("""
93
- # 🌲 **Wild Voices**
94
- *Listening to the More-than-Human World*
95
- Ask questions to rivers, trees, owls, and more. Generate myths and facts, and let nature's wisdom respond.
96
- """)
97
-
98
- # Persona Selection
99
  with gr.Row():
100
  persona1 = gr.Dropdown(personas, label="Choose First Persona", value="Tree")
101
- persona2 = gr.Dropdown(personas + ["Fact-Checker 🧠"], label="Choose Second Persona", value="Crow")
102
 
103
- # Question and buttons
104
  with gr.Row():
105
  user_input = gr.Textbox(label="🌱 Your Question", placeholder="e.g., What do you think of humans?", lines=2)
106
  random_button = gr.Button("🎲 Generate Random Question")
107
  myth_fact_button = gr.Button("πŸ” Generate Random Myth/Fact")
108
 
109
- # Submit button
110
  with gr.Row():
111
  ask_button = gr.Button("🌎 Submit Question")
112
 
113
- # Response display (side by side)
114
  with gr.Row():
115
- output1 = gr.Textbox(label="Response from Persona 1")
116
- output2 = gr.Textbox(label="Response from Persona 2")
117
-
118
- # Update labels when dropdown changes
119
- def update_labels(p1, p2):
120
- label1 = f"{p1} Responds"
121
- label2 = f"{p2} Responds"
122
- output1.label = label1
123
- output2.label = label2
124
- return label1, label2
125
-
126
- persona1.change(fn=update_labels, inputs=[persona1, persona2], outputs=[output1, output2])
127
- persona2.change(fn=update_labels, inputs=[persona1, persona2], outputs=[output1, output2])
128
 
129
  # Button events
130
  random_button.click(fn=get_random_question, inputs=[], outputs=[user_input])
@@ -137,16 +115,9 @@ with gr.Blocks() as demo:
137
  )
138
 
139
  # Ask button for normal questions
140
- def ask_with_context(p1, p2, q):
141
- res1 = call_cohere_api(load_instruction(p1), q)
142
- if p2 == "Fact-Checker 🧠":
143
- res2 = f"I am here to verify myths and facts. Ask me anything related to {p1}."
144
- else:
145
- res2 = call_cohere_api(load_instruction(p2), q)
146
- return res1, res2
147
-
148
  ask_button.click(
149
- fn=ask_with_context,
 
150
  inputs=[persona1, persona2, user_input],
151
  outputs=[output1, output2]
152
  )
 
58
  # Generate Random Myth or Fact and trigger persona response
59
  def get_random_myth_or_fact():
60
  if counternarratives.empty:
61
+ return "No myths or facts available.", "Fact-Checker", "", "", ""
62
 
63
  # πŸ”„ Randomly select a row from the dataframe
64
  row = counternarratives.sample(1).iloc[0]
 
76
  else:
77
  fact_check_response = f"βœ… **FACT**\n\nIndeed, {row['fact']}"
78
 
79
+ # Return the myth/fact, update the personas, and fill the responses
80
+ return myth_or_fact, persona, "Fact-Checker", persona_response, fact_check_response
81
 
82
  # Dynamically load persona names from instructions folder
83
  personas = [os.path.splitext(f)[0].capitalize() for f in os.listdir("instructions") if f.endswith(".txt")]
84
 
85
  # Gradio Interface
86
  with gr.Blocks() as demo:
 
87
  with gr.Row():
88
+ gr.Markdown("# 🌲 **Wild Voices** β€” *Listening to the More-than-Human World*")
89
+ gr.Markdown("Ask questions to rivers, trees, owls, and more. Generate myths and facts, and let nature's wisdom respond.")
90
+
 
 
 
 
 
 
 
91
  with gr.Row():
92
  persona1 = gr.Dropdown(personas, label="Choose First Persona", value="Tree")
93
+ persona2 = gr.Dropdown(personas, label="Choose Second Persona", value="Crow")
94
 
 
95
  with gr.Row():
96
  user_input = gr.Textbox(label="🌱 Your Question", placeholder="e.g., What do you think of humans?", lines=2)
97
  random_button = gr.Button("🎲 Generate Random Question")
98
  myth_fact_button = gr.Button("πŸ” Generate Random Myth/Fact")
99
 
 
100
  with gr.Row():
101
  ask_button = gr.Button("🌎 Submit Question")
102
 
 
103
  with gr.Row():
104
+ output1 = gr.Textbox(label=f"{persona1.value} Responds")
105
+ output2 = gr.Textbox(label="Fact-Checker Responds")
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  # Button events
108
  random_button.click(fn=get_random_question, inputs=[], outputs=[user_input])
 
115
  )
116
 
117
  # Ask button for normal questions
 
 
 
 
 
 
 
 
118
  ask_button.click(
119
+ fn=lambda p1, p2, q: (call_cohere_api(load_instruction(p1), q),
120
+ call_cohere_api(load_instruction(p2), q)),
121
  inputs=[persona1, persona2, user_input],
122
  outputs=[output1, output2]
123
  )