thak123 commited on
Commit
2724867
·
1 Parent(s): f83f10a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -41
app.py CHANGED
@@ -79,47 +79,63 @@
79
  # iface.launch()
80
 
81
  import gradio as gr
82
- import random
83
-
84
- # Sample data
85
- data = [
86
- {
87
- 'word': 'Apple',
88
- 'image_url': 'https://example.com/apple.jpg',
89
- 'translation': 'Manzana',
90
- 'website_link': 'https://en.wikipedia.org/wiki/Apple'
91
- },
92
- {
93
- 'word': 'Banana',
94
- 'image_url': 'https://example.com/banana.jpg',
95
- 'translation': 'Plátano',
96
- 'website_link': 'https://en.wikipedia.org/wiki/Banana'
97
- },
98
- {
99
- 'word': 'Cherry',
100
- 'image_url': 'https://example.com/cherry.jpg',
101
- 'translation': 'Cereza',
102
- 'website_link': 'https://en.wikipedia.org/wiki/Cherry'
103
- }
104
- ]
105
-
106
- # Function to display data for a given index
107
- def display_data(index):
108
- word_data = data[index]
109
- return f"Word: {word_data['word']}\nTranslation: {word_data['translation']}", word_data['image_url'], word_data['website_link']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  # Create a Gradio interface
112
- iface = gr.Interface(
113
- display_data,
114
- [
115
- gr.component(label="Word and Translation", type="text", name="word_translation"),
116
- gr.component(label="Image", type="image", name="image"),
117
- gr.component(label="Website Link", type="text", name="website_link"),
118
- ],
119
- live=False, # To avoid auto-refresh when input changes
120
- examples=[(0,), (1,), (2,)], # Provide initial examples
121
- title="Word Display App"
122
- )
123
-
124
- iface.launch()
125
 
 
79
  # iface.launch()
80
 
81
  import gradio as gr
82
+ import requests
83
+ from PIL import Image
84
+
85
+ def display_item(index, list_of_items):
86
+ """Displays a single item from a list of items.
87
+
88
+ Args:
89
+ index: The index of the item to display.
90
+ list_of_items: A list of Python dictionaries, where each dictionary has the following keys:
91
+ * text: The text.
92
+ * image_url: The URL of the image.
93
+ * translation: The translation of the text.
94
+ * link: The URL of the website link.
95
+ """
96
+
97
+ # Get the item from the list of items
98
+ item = list_of_items[index]
99
+
100
+ # Display the text
101
+ gr.Text(item["text"])
102
+
103
+ # Display the image
104
+ image = Image.open(requests.get(item["image_url"], stream=True).raw)
105
+ gr.Image(image)
106
+
107
+ # Display the translation
108
+ gr.Text(item["translation"])
109
+
110
+ # Display the website link
111
+ gr.Button("View website", onclick=lambda: gr.HTML(f"<iframe src='{item['link']}' width='100%' height='500px'></iframe>"))
112
+
113
+ # Define a function to show the next item
114
+ def show_next_item(index, list_of_items):
115
+ """Shows the next item in the list of items.
116
+
117
+ Args:
118
+ index: The current index.
119
+ list_of_items: A list of Python dictionaries, where each dictionary has the following keys:
120
+ * text: The text.
121
+ * image_url: The URL of the image.
122
+ * translation: The translation of the text.
123
+ * link: The URL of the website link.
124
+ """
125
+
126
+ # Increment the index
127
+ index += 1
128
+
129
+ # If the index is greater than the length of the list of items, then reset the index to 0
130
+ if index >= len(list_of_items):
131
+ index = 0
132
+
133
+ # Display the next item
134
+ display_item(index, list_of_items)
135
 
136
  # Create a Gradio interface
137
+ interface = gr.Interface(display_item, inputs=["number", "list"], outputs="html")
138
+
139
+ # Launch the Gradio interface
140
+ interface.launch(index=0, list_of_items=list_of_items, show_next_item=show_next_item)
 
 
 
 
 
 
 
 
 
141