szili2011 commited on
Commit
c5d3991
·
verified ·
1 Parent(s): 8b7429d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -12,7 +12,7 @@ except Exception as e:
12
 
13
  def ai_vote(poll_title, choices, num_ais):
14
  if chat_pipeline is None:
15
- return {"Error": "Model not initialized"}, []
16
 
17
  # Initialize results and explanations
18
  results = {choice: 0 for choice in choices}
@@ -23,7 +23,6 @@ def ai_vote(poll_title, choices, num_ais):
23
  input_text = f"Poll Title: {poll_title}\nChoices: {', '.join(choices)}\nChoose the best option and explain why."
24
  try:
25
  response = chat_pipeline(input_text, max_length=150, num_return_sequences=1)[0]['generated_text']
26
- # Extract the choice and explanation
27
  for choice in choices:
28
  if choice.lower() in response.lower():
29
  results[choice] += 1
@@ -31,17 +30,30 @@ def ai_vote(poll_title, choices, num_ais):
31
  explanations.append((choice, explanation))
32
  break
33
  except Exception as e:
34
- return {"Error": str(e)}, []
35
 
36
- return results, explanations
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  def gradio_interface(title, choices, num_ais):
39
  try:
40
  choices = [choice.strip() for choice in choices.split(",")]
41
- results, explanations = ai_vote(title, choices, num_ais)
42
- return results, explanations
43
  except Exception as e:
44
- return {"Error": str(e)}, "An error occurred."
45
 
46
  # Gradio Interface
47
  interface = gr.Interface(
@@ -52,8 +64,8 @@ interface = gr.Interface(
52
  gr.Slider(label="Number of AIs", minimum=1, maximum=10, step=1)
53
  ],
54
  outputs=[
55
- gr.JSON(label="Poll Results"),
56
- gr.Textbox(label="AI Explanations")
57
  ]
58
  )
59
 
 
12
 
13
  def ai_vote(poll_title, choices, num_ais):
14
  if chat_pipeline is None:
15
+ return "Error: Model not initialized.", ""
16
 
17
  # Initialize results and explanations
18
  results = {choice: 0 for choice in choices}
 
23
  input_text = f"Poll Title: {poll_title}\nChoices: {', '.join(choices)}\nChoose the best option and explain why."
24
  try:
25
  response = chat_pipeline(input_text, max_length=150, num_return_sequences=1)[0]['generated_text']
 
26
  for choice in choices:
27
  if choice.lower() in response.lower():
28
  results[choice] += 1
 
30
  explanations.append((choice, explanation))
31
  break
32
  except Exception as e:
33
+ return f"Error: {str(e)}", ""
34
 
35
+ # Convert results to HTML for styled output
36
+ styled_results = f"<h2>{poll_title}</h2>"
37
+ styled_results += "<ul>"
38
+ for choice, votes in results.items():
39
+ styled_results += f"<li><strong>{choice}</strong>: {votes} votes</li>"
40
+ styled_results += "</ul>"
41
+
42
+ # Add explanations in HTML
43
+ styled_results += "<h3>AI Explanations:</h3><ul>"
44
+ for choice, explanation in explanations:
45
+ styled_results += f"<li><strong>{choice}:</strong> {explanation}</li>"
46
+ styled_results += "</ul>"
47
+
48
+ return styled_results, explanations
49
 
50
  def gradio_interface(title, choices, num_ais):
51
  try:
52
  choices = [choice.strip() for choice in choices.split(",")]
53
+ styled_results, explanations = ai_vote(title, choices, num_ais)
54
+ return styled_results, explanations
55
  except Exception as e:
56
+ return f"Error: {str(e)}", ""
57
 
58
  # Gradio Interface
59
  interface = gr.Interface(
 
64
  gr.Slider(label="Number of AIs", minimum=1, maximum=10, step=1)
65
  ],
66
  outputs=[
67
+ gr.HTML(label="Poll Results"), # Styled Output
68
+ gr.Textbox(label="Raw AI Explanations")
69
  ]
70
  )
71