davanstrien HF Staff commited on
Commit
da5ada1
·
verified ·
1 Parent(s): 846e7bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -54
app.py CHANGED
@@ -94,74 +94,43 @@ def generate_summary(card_text: str, card_type: str) -> str:
94
  """Cached wrapper for generate_summary with TTL."""
95
  return _generate_summary_gpu(card_text, card_type)
96
 
97
- def summarize(hub_id: str = "", card_type: str = "model", content: str = "") -> str:
98
- """Interface function for Gradio."""
99
  try:
100
  if hub_id:
101
  # Fetch and validate card type
102
  inferred_type, card_text = get_card_info(hub_id)
103
  if card_type and card_type != inferred_type:
104
- return f"Error: Provided card_type '{card_type}' doesn't match inferred type '{inferred_type}'"
 
105
  card_type = inferred_type
106
- elif content:
107
- if not card_type:
108
- return "Error: card_type must be provided when using direct content"
109
- card_text = content
110
  else:
111
- return "Error: Either hub_id or content must be provided"
 
112
 
113
  # Use the cached wrapper
114
  summary = generate_summary(card_text, card_type)
115
- return summary
 
116
 
117
  except Exception as e:
118
- return f"Error: {str(e)}"
 
119
 
120
- # Create the Gradio interface
121
  def create_interface():
122
- with gr.Blocks(title="Hub TLDR") as interface:
123
- gr.Markdown("# Hugging Face Hub TLDR Generator")
124
- gr.Markdown("Generate concise summaries of model and dataset cards from the Hugging Face Hub.")
125
-
126
- with gr.Tab("Summarize by Hub ID"):
127
- hub_id_input = gr.Textbox(
128
- label="Hub ID",
129
- placeholder="e.g., huggingface/llama-7b"
130
- )
131
- hub_id_type = gr.Radio(
132
- choices=["model", "dataset"],
133
- label="Card Type (optional)",
134
- value="model"
135
- )
136
- hub_id_button = gr.Button("Generate Summary")
137
- hub_id_output = gr.Textbox(label="Summary")
138
-
139
- hub_id_button.click(
140
- fn=summarize,
141
- inputs=[hub_id_input, hub_id_type],
142
- outputs=hub_id_output
143
- )
144
-
145
- with gr.Tab("Summarize Custom Content"):
146
- content_input = gr.Textbox(
147
- label="Content",
148
- placeholder="Paste your model or dataset card content here...",
149
- lines=10
150
- )
151
- content_type = gr.Radio(
152
- choices=["model", "dataset"],
153
- label="Card Type",
154
- value="model"
155
- )
156
- content_button = gr.Button("Generate Summary")
157
- content_output = gr.Textbox(label="Summary")
158
-
159
- content_button.click(
160
- fn=lambda content, card_type: summarize(content=content, card_type=card_type),
161
- inputs=[content_input, content_type],
162
- outputs=content_output
163
- )
164
-
165
  return interface
166
 
167
  if __name__ == "__main__":
 
94
  """Cached wrapper for generate_summary with TTL."""
95
  return _generate_summary_gpu(card_text, card_type)
96
 
97
+ def summarize(hub_id: str = "", card_type: str = "model") -> Tuple[str, str]:
98
+ """Interface function for Gradio. Returns both text and JSON formats."""
99
  try:
100
  if hub_id:
101
  # Fetch and validate card type
102
  inferred_type, card_text = get_card_info(hub_id)
103
  if card_type and card_type != inferred_type:
104
+ error_msg = f"Error: Provided card_type '{card_type}' doesn't match inferred type '{inferred_type}'"
105
+ return error_msg, f'{{"error": "{error_msg}"}}'
106
  card_type = inferred_type
 
 
 
 
107
  else:
108
+ error_msg = "Error: Hub ID must be provided"
109
+ return error_msg, f'{{"error": "{error_msg}"}}'
110
 
111
  # Use the cached wrapper
112
  summary = generate_summary(card_text, card_type)
113
+ json_output = f'{{"summary": "{summary}", "type": "{card_type}", "hub_id": "{hub_id}"}}'
114
+ return summary, json_output
115
 
116
  except Exception as e:
117
+ error_msg = str(e)
118
+ return f"Error: {error_msg}", f'{{"error": "{error_msg}"}}'
119
 
 
120
  def create_interface():
121
+ interface = gr.Interface(
122
+ fn=summarize,
123
+ inputs=[
124
+ gr.Textbox(label="Hub ID", placeholder="e.g., huggingface/llama-7b"),
125
+ gr.Radio(choices=["model", "dataset"], label="Card Type", value="model"),
126
+ ],
127
+ outputs=[
128
+ gr.Textbox(label="Summary"),
129
+ gr.JSON(label="JSON Output")
130
+ ],
131
+ title="Hugging Face Hub TLDR Generator",
132
+ description="Generate concise summaries of model and dataset cards from the Hugging Face Hub.",
133
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  return interface
135
 
136
  if __name__ == "__main__":