szili2011 commited on
Commit
6ffa12b
·
verified ·
1 Parent(s): 39ee408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py CHANGED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Add a print statement to confirm initialization
5
+ print("Initializing model...")
6
+
7
+ # Load AI model
8
+ try:
9
+ model = pipeline("text2text-generation", model="google/flan-t5-base")
10
+ print("Model loaded successfully.")
11
+ except Exception as e:
12
+ print(f"Error loading model: {e}")
13
+
14
+ def ai_vote(poll_title, choices, num_ais):
15
+ try:
16
+ results = {}
17
+ explanations = []
18
+
19
+ for _ in range(num_ais):
20
+ input_text = f"Poll Title: {poll_title}\nChoices: {', '.join(choices)}\nChoose the best option and explain why."
21
+ response = model(input_text, max_length=100, num_return_sequences=1)[0]['generated_text']
22
+
23
+ # Extract the chosen option and explanation
24
+ chosen_option = response.split("\n")[0].strip()
25
+ explanation = "\n".join(response.split("\n")[1:]).strip()
26
+
27
+ results[chosen_option] = results.get(chosen_option, 0) + 1
28
+ explanations.append((chosen_option, explanation))
29
+
30
+ return results, explanations
31
+ except Exception as e:
32
+ return {"Error": str(e)}, []
33
+
34
+ # Gradio interface
35
+ def gradio_interface(title, choices, num_ais):
36
+ try:
37
+ choices = [choice.strip() for choice in choices.split(",")]
38
+ results, explanations = ai_vote(title, choices, num_ais)
39
+ return results, explanations
40
+ except Exception as e:
41
+ return {"Error": str(e)}, "An error occurred."
42
+
43
+ interface = gr.Interface(
44
+ fn=gradio_interface,
45
+ inputs=[
46
+ gr.Textbox(label="Poll Title"),
47
+ gr.Textbox(label="Choices (comma-separated)"),
48
+ gr.Slider(label="Number of AIs", minimum=1, maximum=10, step=1)
49
+ ],
50
+ outputs=[
51
+ gr.Label(label="Poll Results"),
52
+ gr.Textbox(label="AI Explanations")
53
+ ]
54
+ )
55
+
56
+ # Launch app
57
+ if __name__ == "__main__":
58
+ print("Launching interface...")
59
+ interface.launch()