Spaces:
Running
Running
File size: 62,259 Bytes
9b95875 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 |
import gradio as gr
import torch
import os
from transformers import AutoTokenizer, AutoModelForCausalLM
import random
from gradio.themes.utils import colors, sizes
from gradio.themes import Base, Soft
# Create custom themes for light and dark mode
class GemmaLightTheme(Soft):
def __init__(self):
super().__init__(
primary_hue=colors.blue,
secondary_hue=colors.indigo,
neutral_hue=colors.gray,
spacing_size=sizes.spacing_md,
radius_size=sizes.radius_md,
text_size=sizes.text_md,
)
class GemmaDarkTheme(Soft):
def __init__(self):
super().__init__(
primary_hue=colors.blue,
secondary_hue=colors.indigo,
neutral_hue=colors.gray,
spacing_size=sizes.spacing_md,
radius_size=sizes.radius_md,
text_size=sizes.text_md,
)
self.name = "gemma_dark"
# Make it dark
self.background_fill_primary = "#1F1F2E"
self.background_fill_secondary = "#2A2A3C"
self.border_color_primary = "#3A3A4C"
self.border_color_secondary = "#4A4A5C"
self.color_accent_soft = "#3B5FA3"
self.color_accent = "#4B82C4"
self.text_color = "#FFFFFF"
self.text_color_subdued = "#CCCCCC"
self.shadow_spread = "8px"
self.shadow_inset = "0px 1px 2px 0px rgba(0, 0, 0, 0.1) inset"
# Helper function to handle empty values
def safe_value(value, default):
"""Return default if value is empty or None"""
if value is None or value == "":
return default
return value
# Get Hugging Face token from environment variable (as fallback)
DEFAULT_HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN", None)
# Create global variables for model and tokenizer
global_model = None
global_tokenizer = None
model_loaded = False
def load_model(hf_token):
"""Load the model with the provided token"""
global global_model, global_tokenizer, model_loaded
if not hf_token:
model_loaded = False
return "β οΈ Please enter your Hugging Face token to use the model."
try:
# Try different model versions from smallest to largest
model_options = [
"google/gemma-2b-it", # Try an instruction-tuned 2B model first (smallest)
"google/gemma-2b", # Try base 2B model next
"google/gemma-7b-it", # Try 7B instruction-tuned model
"google/gemma-7b", # Try base 7B model
]
print(f"Attempting to load models with token starting with: {hf_token[:5]}...")
# Try to load models in order until one works
for model_name in model_options:
try:
print(f"Attempting to load model: {model_name}")
# Load tokenizer
print("Loading tokenizer...")
global_tokenizer = AutoTokenizer.from_pretrained(
model_name,
token=hf_token
)
print("Tokenizer loaded successfully")
# Load model with minimal configuration
print(f"Loading model {model_name}...")
global_model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto",
token=hf_token
)
print(f"Model {model_name} loaded successfully!")
model_loaded = True
return f"β
Model {model_name} loaded successfully!"
except Exception as specific_e:
print(f"Failed to load {model_name}: {specific_e}")
import traceback
traceback.print_exc()
continue
# If we get here, all model options failed - try one more option with no token
try:
print("Trying a public model with no token requirement...")
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
global_tokenizer = AutoTokenizer.from_pretrained(model_name)
global_model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto"
)
model_loaded = True
return f"β
Fallback model {model_name} loaded successfully! Note: This is not Gemma but a fallback model."
except Exception as fallback_e:
print(f"Failed to load fallback model: {fallback_e}")
# If we get here, all model options failed
model_loaded = False
return "β Could not load any model version. Please check your token and try again."
except Exception as e:
model_loaded = False
error_msg = str(e)
print(f"Error in load_model: {error_msg}")
import traceback
traceback.print_exc()
if "401 Client Error" in error_msg:
return "β Authentication failed. Please check your token and make sure you've accepted the model license on Hugging Face."
else:
return f"β Error loading model: {error_msg}"
def generate_prompt(task_type, **kwargs):
"""Generate appropriate prompts based on task type and parameters"""
if task_type == "creative":
style = kwargs.get("style", "")
topic = kwargs.get("topic", "")
return f"Write a {style} about {topic}. Be creative and engaging."
elif task_type == "informational":
format_type = kwargs.get("format_type", "")
topic = kwargs.get("topic", "")
return f"Write an {format_type} about {topic}. Be clear, factual, and informative."
elif task_type == "summarize":
text = kwargs.get("text", "")
return f"Summarize the following text in a concise way:\n\n{text}"
elif task_type == "translate":
text = kwargs.get("text", "")
target_lang = kwargs.get("target_lang", "")
return f"Translate the following text to {target_lang}:\n\n{text}"
elif task_type == "qa":
text = kwargs.get("text", "")
question = kwargs.get("question", "")
return f"Based on the following text:\n\n{text}\n\nAnswer this question: {question}"
elif task_type == "code_generate":
language = kwargs.get("language", "")
task = kwargs.get("task", "")
return f"Write {language} code to {task}. Include helpful comments."
elif task_type == "code_explain":
code = kwargs.get("code", "")
return f"Explain what the following code does in simple terms:\n\n```\n{code}\n```"
elif task_type == "code_debug":
code = kwargs.get("code", "")
return f"The following code has an issue. Identify and fix the problem:\n\n```\n{code}\n```"
elif task_type == "brainstorm":
topic = kwargs.get("topic", "")
category = kwargs.get("category", "")
return f"Brainstorm {category} ideas about {topic}. Provide a diverse list of options."
elif task_type == "content_creation":
content_type = kwargs.get("content_type", "")
topic = kwargs.get("topic", "")
audience = kwargs.get("audience", "")
return f"Create a {content_type} about {topic} for {audience}. Make it engaging and relevant."
elif task_type == "email_draft":
email_type = kwargs.get("email_type", "")
context = kwargs.get("context", "")
return f"Write a professional {email_type} email with the following context:\n\n{context}"
elif task_type == "document_edit":
text = kwargs.get("text", "")
edit_type = kwargs.get("edit_type", "")
return f"Improve the following text for {edit_type}:\n\n{text}"
elif task_type == "explain":
topic = kwargs.get("topic", "")
level = kwargs.get("level", "")
return f"Explain {topic} in a way that's easy to understand for a {level} audience."
elif task_type == "classify":
text = kwargs.get("text", "")
categories = kwargs.get("categories", "")
return f"Classify the following text into one of these categories: {categories}\n\nText: {text}\n\nCategory:"
elif task_type == "data_extract":
text = kwargs.get("text", "")
data_points = kwargs.get("data_points", "")
return f"Extract the following information from the text: {data_points}\n\nText: {text}\n\nExtracted information:"
else:
return kwargs.get("prompt", "")
def generate_text(prompt, max_length=1024, temperature=0.7, top_p=0.95):
"""Generate text using the Gemma model"""
global global_model, global_tokenizer, model_loaded
print(f"Generating text with params: max_length={max_length}, temp={temperature}, top_p={top_p}")
print(f"Prompt: {prompt[:100]}...")
if not model_loaded or global_model is None or global_tokenizer is None:
print("Model not loaded")
return "β οΈ Model not loaded. Please authenticate with your Hugging Face token."
if not prompt:
return "Please enter a prompt to generate text."
try:
# Keep generation simple to avoid errors
inputs = global_tokenizer(prompt, return_tensors="pt").to(global_model.device)
print(f"Input token length: {len(inputs.input_ids[0])}")
# Use even simpler generation parameters
generation_args = {
"input_ids": inputs.input_ids,
"max_length": min(2048, max_length + len(inputs.input_ids[0])),
"do_sample": True,
}
# Only add temperature if not too low (can cause issues)
if temperature >= 0.3:
generation_args["temperature"] = temperature
print(f"Generation args: {generation_args}")
# Generate text
outputs = global_model.generate(**generation_args)
# Decode and return the generated text
generated_text = global_tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Generated text length: {len(generated_text)}")
return generated_text
except Exception as e:
error_msg = str(e)
print(f"Generation error: {error_msg}")
print(f"Error type: {type(e)}")
import traceback
traceback.print_exc()
if "probability tensor" in error_msg:
return "Error: There was a problem with the generation parameters. Try using simpler parameters or a different prompt."
else:
return f"Error generating text: {error_msg}"
# Create parameters UI component
def create_parameter_ui():
with gr.Row(equal_height=True):
with gr.Column(scale=1):
max_length = gr.Slider(
minimum=128,
maximum=2048,
value=1024,
step=64,
label="Maximum Length",
info="Control the maximum length of generated text",
elem_id="max_length_slider"
)
with gr.Column(scale=1):
temperature = gr.Slider(
minimum=0.3,
maximum=1.5,
value=0.8,
step=0.1,
label="Temperature",
info="Higher values create more creative but potentially less coherent outputs",
elem_id="temperature_slider"
)
with gr.Column(scale=1):
top_p = gr.Slider(
minimum=0.5,
maximum=0.99,
value=0.9,
step=0.05,
label="Top-p",
info="Controls diversity of generated text",
elem_id="top_p_slider"
)
return [max_length, temperature, top_p]
# Create Gradio interface
with gr.Blocks(theme=GemmaLightTheme()) as demo:
# Header with theme toggle
with gr.Row(equal_height=True):
with gr.Column(scale=6):
gr.Markdown(
"""
# π€ Gemma Capabilities Demo
This interactive demo showcases Google's Gemma model capabilities across different tasks.
"""
)
with gr.Column(scale=1, min_width=150):
theme_toggle = gr.Radio(
["Light", "Dark"],
value="Light",
label="Theme",
info="Switch between light and dark mode",
elem_id="theme_toggle"
)
# Add CSS for themes and set up JavaScript for theme toggle
gr.HTML("""
<style>
/* Dark theme styles */
.dark-theme {
--background-fill-primary: #1F1F2E !important;
--background-fill-secondary: #2A2A3C !important;
--border-color-primary: #3A3A4C !important;
--border-color-secondary: #4A4A5C !important;
--color-accent: #4B82C4 !important;
--color-accent-soft: #3B5FA3 !important;
--color-text: #FFFFFF !important;
--color-subtext: #CCCCCC !important;
}
</style>
<script>
// This script will run after the page loads
(function() {
// Function to check the theme toggle and apply the appropriate theme
function applyTheme() {
const themeToggle = document.getElementById('theme_toggle');
if (!themeToggle) return;
const inputs = themeToggle.querySelectorAll('input');
for (let input of inputs) {
if (input.checked && input.value === 'Dark') {
document.body.classList.add('dark-theme');
return;
}
}
// If we get here, light theme is selected or no selection
document.body.classList.remove('dark-theme');
}
// Set up a mutation observer to watch for changes to the radio buttons
function setupObserver() {
const themeToggle = document.getElementById('theme_toggle');
if (!themeToggle) {
// Try again in a bit if the element isn't ready yet
setTimeout(setupObserver, 100);
return;
}
// Apply theme initially
applyTheme();
// Watch for changes
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'attributes' || mutation.type === 'childList') {
applyTheme();
}
});
});
observer.observe(themeToggle, {
attributes: true,
childList: true,
subtree: true
});
// Also add click listeners to ensure theme changes on click
const inputs = themeToggle.querySelectorAll('input');
inputs.forEach(input => {
input.addEventListener('change', applyTheme);
});
}
// Start the setup when the document is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', setupObserver);
} else {
setupObserver();
}
})();
</script>
""")
# Authentication Section
with gr.Group(elem_id="auth_box"):
gr.Markdown("## π Authentication", elem_id="auth_heading")
with gr.Row(equal_height=True):
with gr.Column(scale=3):
hf_token = gr.Textbox(
label="Hugging Face Token",
placeholder="Enter your token here...",
type="password",
value=DEFAULT_HF_TOKEN,
info="Get your token from https://huggingface.co/settings/tokens",
elem_id="hf_token_input"
)
with gr.Column(scale=1):
auth_button = gr.Button("Authenticate", variant="primary", elem_id="auth_button")
auth_status = gr.Markdown("Please authenticate to use the model.", elem_id="auth_status")
def authenticate(token):
return "β³ Loading model... Please wait, this may take a minute."
def auth_complete(token):
result = load_model(token)
return result
# Two-step authentication to show loading message
auth_button.click(
fn=authenticate,
inputs=[hf_token],
outputs=[auth_status],
queue=False
).then(
fn=auth_complete,
inputs=[hf_token],
outputs=[auth_status]
)
gr.Markdown(
"""
### How to get a token:
1. Go to [Hugging Face Token Settings](https://huggingface.co/settings/tokens)
2. Create a new token with read access
3. Make sure you've accepted the [Gemma model license](https://huggingface.co/google/gemma-3-4b-pt)
"""
)
# Main content - only show when authenticated
with gr.Tabs(elem_id="main_tabs") as tabs:
# Text Generation Tab
with gr.TabItem("Text Generation", id="tab_text_gen"):
gr.Markdown(
"""
## βοΈ Creative Text Generation
Generate stories, poems, and other creative content. Choose a style and topic or enter your own prompt.
"""
)
with gr.Group(elem_id="text_gen_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
text_gen_type = gr.Radio(
["Creative Writing", "Informational Writing", "Custom Prompt"],
label="Writing Type",
value="Creative Writing",
elem_id="text_gen_type"
)
# Creative writing options
with gr.Group(visible=True, elem_id="creative_options") as creative_options:
style = gr.Dropdown(
["short story", "poem", "script", "song lyrics", "joke"],
label="Style",
value="short story",
elem_id="creative_style"
)
creative_topic = gr.Textbox(
label="Topic",
placeholder="Enter a topic...",
value="a robot discovering emotions",
elem_id="creative_topic"
)
# Informational writing options
with gr.Group(visible=False, elem_id="info_options") as info_options:
format_type = gr.Dropdown(
["article", "summary", "explanation", "report"],
label="Format",
value="article",
elem_id="info_format"
)
info_topic = gr.Textbox(
label="Topic",
placeholder="Enter a topic...",
value="artificial intelligence",
elem_id="info_topic"
)
# Custom prompt
with gr.Group(visible=False, elem_id="custom_prompt_group") as custom_prompt_group:
custom_prompt = gr.Textbox(
label="Custom Prompt",
placeholder="Enter your custom prompt...",
lines=3,
elem_id="custom_prompt"
)
# Show/hide options based on selection
def update_text_gen_visibility(choice):
return {
creative_options: choice == "Creative Writing",
info_options: choice == "Informational Writing",
custom_prompt_group: choice == "Custom Prompt"
}
text_gen_type.change(
update_text_gen_visibility,
inputs=text_gen_type,
outputs=[creative_options, info_options, custom_prompt_group]
)
# Generation parameters
with gr.Accordion("Advanced Parameters", open=False, elem_id="text_gen_params"):
text_gen_params = create_parameter_ui()
generate_text_btn = gr.Button("Generate", variant="primary", size="lg", elem_id="generate_text_btn")
with gr.Column(scale=1):
text_output = gr.Textbox(
label="Generated Text",
lines=20,
elem_id="text_output"
)
# Handle text generation
def text_generation_handler(
gen_type, style, creative_topic, format_type, info_topic,
custom_prompt, max_length, temperature, top_p
):
if gen_type == "Creative Writing":
style = safe_value(style, "short story")
creative_topic = safe_value(creative_topic, "a story")
prompt = generate_prompt("creative", style=style, topic=creative_topic)
elif gen_type == "Informational Writing":
format_type = safe_value(format_type, "article")
info_topic = safe_value(info_topic, "a topic")
prompt = generate_prompt("informational", format_type=format_type, topic=info_topic)
else:
prompt = safe_value(custom_prompt, "Write something interesting")
return generate_text(prompt, max_length, temperature, top_p)
generate_text_btn.click(
text_generation_handler,
inputs=[
text_gen_type, style, creative_topic, format_type, info_topic,
custom_prompt, *text_gen_params
],
outputs=text_output
)
# Examples for text generation
gr.Examples(
[
["Creative Writing", "short story", "a robot learning to paint", "article", "artificial intelligence", "", 1024, 0.8, 0.9],
["Creative Writing", "poem", "the beauty of mathematics", "article", "artificial intelligence", "", 768, 0.8, 0.9],
["Informational Writing", "short story", "a robot discovering emotions", "article", "quantum computing", "", 1024, 0.7, 0.9],
["Custom Prompt", "short story", "a robot discovering emotions", "article", "artificial intelligence", "Write a marketing email for a new smartphone with innovative AI features", 1024, 0.8, 0.9]
],
fn=text_generation_handler,
inputs=[
text_gen_type, style, creative_topic, format_type, info_topic,
custom_prompt, *text_gen_params
],
outputs=text_output,
label="Examples"
)
# Brainstorming Tab
with gr.TabItem("Brainstorming", id="tab_brainstorm"):
gr.Markdown(
"""
## π§ Brainstorming Ideas
Generate creative ideas for projects, solutions, or any topic you're interested in.
"""
)
with gr.Group(elem_id="brainstorm_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
brainstorm_category = gr.Dropdown(
["project", "business", "creative", "solution", "content", "feature", "product"],
label="Category",
value="project",
elem_id="brainstorm_category"
)
brainstorm_topic = gr.Textbox(
label="Topic or Problem",
placeholder="What would you like ideas for?",
value="sustainable technology",
elem_id="brainstorm_topic"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="brainstorm_params"):
brainstorm_params = create_parameter_ui()
brainstorm_btn = gr.Button("Generate Ideas", variant="primary", size="lg", elem_id="brainstorm_btn")
with gr.Column(scale=1):
brainstorm_output = gr.Textbox(
label="Generated Ideas",
lines=20,
elem_id="brainstorm_output"
)
def brainstorm_handler(category, topic, max_length, temperature, top_p):
category = safe_value(category, "project")
topic = safe_value(topic, "innovative ideas")
prompt = generate_prompt("brainstorm", category=category, topic=topic)
return generate_text(prompt, max_length, temperature, top_p)
brainstorm_btn.click(
brainstorm_handler,
inputs=[brainstorm_category, brainstorm_topic, *brainstorm_params],
outputs=brainstorm_output
)
# Examples for brainstorming
gr.Examples(
[
["project", "educational app for children", 1024, 0.8, 0.9],
["business", "eco-friendly food packaging", 1024, 0.8, 0.9],
["solution", "reducing urban traffic congestion", 1024, 0.8, 0.9],
],
fn=brainstorm_handler,
inputs=[brainstorm_category, brainstorm_topic, *brainstorm_params],
outputs=brainstorm_output,
label="Examples"
)
# Content Creation Tab
with gr.TabItem("Content Creation", id="tab_content"):
gr.Markdown(
"""
## π Content Creation
Generate various types of content such as blog posts, social media updates, marketing copy, etc.
"""
)
with gr.Group(elem_id="content_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
content_type = gr.Dropdown(
["blog post", "social media post", "marketing copy", "product description", "press release", "newsletter"],
label="Content Type",
value="blog post",
elem_id="content_type"
)
content_topic = gr.Textbox(
label="Topic",
placeholder="What is your content about?",
value="the future of artificial intelligence",
elem_id="content_topic"
)
content_audience = gr.Textbox(
label="Target Audience",
placeholder="Who is your audience?",
value="tech enthusiasts",
elem_id="content_audience"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="content_params"):
content_params = create_parameter_ui()
content_btn = gr.Button("Generate Content", variant="primary", size="lg", elem_id="content_btn")
with gr.Column(scale=1):
content_output = gr.Textbox(
label="Generated Content",
lines=20,
elem_id="content_output"
)
def content_creation_handler(content_type, topic, audience, max_length, temperature, top_p):
content_type = safe_value(content_type, "blog post")
topic = safe_value(topic, "interesting topic")
audience = safe_value(audience, "general audience")
prompt = generate_prompt("content_creation", content_type=content_type, topic=topic, audience=audience)
return generate_text(prompt, max_length, temperature, top_p)
content_btn.click(
content_creation_handler,
inputs=[content_type, content_topic, content_audience, *content_params],
outputs=content_output
)
# Examples for content creation
gr.Examples(
[
["blog post", "sustainable living tips", "environmentally conscious consumers", 1536, 0.8, 0.9],
["social media post", "product launch announcement", "existing customers", 512, 0.8, 0.9],
["marketing copy", "new fitness app", "health-focused individuals", 1024, 0.8, 0.9],
],
fn=content_creation_handler,
inputs=[content_type, content_topic, content_audience, *content_params],
outputs=content_output,
label="Examples"
)
# Email Drafting Tab
with gr.TabItem("Email Drafting", id="tab_email"):
gr.Markdown(
"""
## βοΈ Email Drafting
Generate professional email drafts for various purposes.
"""
)
with gr.Group(elem_id="email_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
email_type = gr.Dropdown(
["job application", "customer support", "business proposal", "networking", "follow-up", "thank you", "meeting request"],
label="Email Type",
value="job application",
elem_id="email_type"
)
email_context = gr.Textbox(
label="Context and Details",
placeholder="Provide necessary context for the email...",
lines=5,
value="Applying for a software developer position at Tech Solutions Inc. I have 3 years of experience with Python and JavaScript.",
elem_id="email_context"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="email_params"):
email_params = create_parameter_ui()
email_btn = gr.Button("Generate Email", variant="primary", size="lg", elem_id="email_btn")
with gr.Column(scale=1):
email_output = gr.Textbox(
label="Generated Email",
lines=20,
elem_id="email_output"
)
def email_draft_handler(email_type, context, max_length, temperature, top_p):
email_type = safe_value(email_type, "professional")
context = safe_value(context, "general communication")
prompt = generate_prompt("email_draft", email_type=email_type, context=context)
return generate_text(prompt, max_length, temperature, top_p)
email_btn.click(
email_draft_handler,
inputs=[email_type, email_context, *email_params],
outputs=email_output
)
# Examples for email drafting
gr.Examples(
[
["job application", "Applying for a marketing specialist position at ABC Marketing. I have 5 years of experience in digital marketing.", 1024, 0.8, 0.9],
["business proposal", "Proposing a collaboration between our companies for a joint product development effort.", 1024, 0.8, 0.9],
["follow-up", "Following up after our meeting last Thursday about the project timeline and resources.", 1024, 0.8, 0.9],
],
fn=email_draft_handler,
inputs=[email_type, email_context, *email_params],
outputs=email_output,
label="Examples"
)
# Document Editing Tab
with gr.TabItem("Document Editing", id="tab_edit"):
gr.Markdown(
"""
## βοΈ Document Editing
Improve the clarity, grammar, and style of your writing.
"""
)
with gr.Group(elem_id="edit_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
edit_text = gr.Textbox(
label="Text to Edit",
placeholder="Paste your text here...",
lines=10,
value="The company have been experiencing rapid growth over the past few years and is expecting to continue this trend in the coming years. They believe that it's success is due to the quality of their products and the dedicated team.",
elem_id="edit_text"
)
edit_type = gr.Dropdown(
["grammar and clarity", "conciseness", "formal tone", "casual tone", "simplification", "academic style", "persuasive style"],
label="Edit Type",
value="grammar and clarity",
elem_id="edit_type"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="edit_params"):
edit_params = create_parameter_ui()
edit_btn = gr.Button("Edit Document", variant="primary", size="lg", elem_id="edit_btn")
with gr.Column(scale=1):
edit_output = gr.Textbox(
label="Edited Text",
lines=10,
elem_id="edit_output"
)
def document_edit_handler(text, edit_type, max_length, temperature, top_p):
text = safe_value(text, "Please provide text to edit.")
edit_type = safe_value(edit_type, "grammar and clarity")
prompt = generate_prompt("document_edit", text=text, edit_type=edit_type)
return generate_text(prompt, max_length, temperature, top_p)
edit_btn.click(
document_edit_handler,
inputs=[edit_text, edit_type, *edit_params],
outputs=edit_output
)
# Learning & Explanation Tab
with gr.TabItem("Learning & Explanation", id="tab_explain"):
gr.Markdown(
"""
## π Learning & Explanation
Get easy-to-understand explanations of complex topics.
"""
)
with gr.Group(elem_id="explain_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
explain_topic = gr.Textbox(
label="Topic to Explain",
placeholder="What topic would you like explained?",
value="quantum computing",
elem_id="explain_topic"
)
explain_level = gr.Dropdown(
["beginner", "child", "teenager", "college student", "professional", "expert"],
label="Audience Level",
value="beginner",
elem_id="explain_level"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="explain_params"):
explain_params = create_parameter_ui()
explain_btn = gr.Button("Generate Explanation", variant="primary", size="lg", elem_id="explain_btn")
with gr.Column(scale=1):
explain_output = gr.Textbox(
label="Explanation",
lines=20,
elem_id="explain_output"
)
def explanation_handler(topic, level, max_length, temperature, top_p):
topic = safe_value(topic, "an interesting concept")
level = safe_value(level, "beginner")
prompt = generate_prompt("explain", topic=topic, level=level)
return generate_text(prompt, max_length, temperature, top_p)
explain_btn.click(
explanation_handler,
inputs=[explain_topic, explain_level, *explain_params],
outputs=explain_output
)
# Examples for explanation
gr.Examples(
[
["blockchain technology", "beginner", 1024, 0.8, 0.9],
["photosynthesis", "child", 1024, 0.8, 0.9],
["machine learning", "college student", 1024, 0.8, 0.9],
],
fn=explanation_handler,
inputs=[explain_topic, explain_level, *explain_params],
outputs=explain_output,
label="Examples"
)
# Classification & Categorization Tab
with gr.TabItem("Classification", id="tab_classify"):
gr.Markdown(
"""
## π·οΈ Classification & Categorization
Classify text into different categories or themes.
"""
)
with gr.Group(elem_id="classify_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
classify_text = gr.Textbox(
label="Text to Classify",
placeholder="Enter the text you want to classify...",
lines=8,
value="The latest smartphone features a powerful processor, excellent camera, and impressive battery life, making it a top choice for tech enthusiasts.",
elem_id="classify_text"
)
classify_categories = gr.Textbox(
label="Categories (comma-separated)",
placeholder="List categories separated by commas...",
value="technology, health, finance, entertainment, education, sports",
elem_id="classify_categories"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="classify_params"):
classify_params = create_parameter_ui()
classify_btn = gr.Button("Classify Text", variant="primary", size="lg", elem_id="classify_btn")
with gr.Column(scale=1):
classify_output = gr.Textbox(
label="Classification Result",
lines=5,
elem_id="classify_output"
)
def classification_handler(text, categories, max_length, temperature, top_p):
text = safe_value(text, "Please provide text to classify.")
categories = safe_value(categories, "general, specific, other")
prompt = generate_prompt("classify", text=text, categories=categories)
return generate_text(prompt, max_length, temperature, top_p)
classify_btn.click(
classification_handler,
inputs=[classify_text, classify_categories, *classify_params],
outputs=classify_output
)
# Examples for classification
gr.Examples(
[
["The stock market saw significant gains today as tech companies reported strong quarterly earnings.", "technology, health, finance, entertainment, education, sports", 256, 0.5, 0.9],
["The team scored in the final minutes to secure their victory in the championship game.", "technology, health, finance, entertainment, education, sports", 256, 0.5, 0.9],
["The new educational app helps students master complex math concepts through interactive exercises.", "technology, health, finance, entertainment, education, sports", 256, 0.5, 0.9],
],
fn=classification_handler,
inputs=[classify_text, classify_categories, *classify_params],
outputs=classify_output,
label="Examples"
)
# Data Extraction Tab
with gr.TabItem("Data Extraction", id="tab_extract"):
gr.Markdown(
"""
## π Data Extraction
Extract specific data points from text.
"""
)
with gr.Group(elem_id="extract_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
extract_text = gr.Textbox(
label="Text to Process",
placeholder="Enter the text containing data to extract...",
lines=10,
value="John Smith, born on May 15, 1985, is a software engineer at Tech Solutions Inc. He can be reached at [email protected] or (555) 123-4567. John graduated from MIT in 2007 with a degree in Computer Science.",
elem_id="extract_text"
)
extract_data_points = gr.Textbox(
label="Data Points to Extract (comma-separated)",
placeholder="Specify what data to extract...",
value="name, email, phone number, birth date, company, education",
elem_id="extract_data_points"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="extract_params"):
extract_params = create_parameter_ui()
extract_btn = gr.Button("Extract Data", variant="primary", size="lg", elem_id="extract_btn")
with gr.Column(scale=1):
extract_output = gr.Textbox(
label="Extracted Data",
lines=10,
elem_id="extract_output"
)
def data_extraction_handler(text, data_points, max_length, temperature, top_p):
text = safe_value(text, "Please provide text with data to extract.")
data_points = safe_value(data_points, "key information")
prompt = generate_prompt("data_extract", text=text, data_points=data_points)
return generate_text(prompt, max_length, temperature, top_p)
extract_btn.click(
data_extraction_handler,
inputs=[extract_text, extract_data_points, *extract_params],
outputs=extract_output
)
# Examples for data extraction
gr.Examples(
[
["Sarah Johnson is the CEO of Green Innovations, founded in 2012. The company reported $8.5 million in revenue for 2023. Contact her at [email protected].", "name, position, company, founding year, revenue, contact", 768, 0.5, 0.9],
["The new iPhone 15 Pro features a 6.1-inch display, A17 Pro chip, 48MP camera, and starts at $999 for the 128GB model.", "product name, screen size, processor, camera, price, storage capacity", 768, 0.5, 0.9],
],
fn=data_extraction_handler,
inputs=[extract_text, extract_data_points, *extract_params],
outputs=extract_output,
label="Examples"
)
# Text Comprehension Tab
with gr.TabItem("Text Comprehension", id="tab_comprehension"):
gr.Markdown(
"""
## π Text Comprehension
Test Gemma's ability to understand and process text. Try summarization, Q&A, or translation.
"""
)
with gr.Tabs() as comprehension_tabs:
# Summarization
with gr.TabItem("Summarization", id="subtab_summarize"):
with gr.Group(elem_id="summarize_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
summarize_text = gr.Textbox(
label="Text to Summarize",
placeholder="Paste text here...",
lines=10,
elem_id="summarize_text"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="summarize_params"):
summarize_params = create_parameter_ui()
summarize_btn = gr.Button("Summarize", variant="primary", size="lg", elem_id="summarize_btn")
with gr.Column(scale=1):
summary_output = gr.Textbox(
label="Summary",
lines=10,
elem_id="summary_output"
)
# Question Answering
with gr.TabItem("Question Answering", id="subtab_qa"):
with gr.Group(elem_id="qa_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
qa_text = gr.Textbox(
label="Context Text",
placeholder="Paste text here...",
lines=10,
elem_id="qa_text"
)
qa_question = gr.Textbox(
label="Question",
placeholder="Ask a question about the text...",
elem_id="qa_question"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="qa_params"):
qa_params = create_parameter_ui()
qa_btn = gr.Button("Answer", variant="primary", size="lg", elem_id="qa_btn")
with gr.Column(scale=1):
qa_output = gr.Textbox(
label="Answer",
lines=10,
elem_id="qa_output"
)
# Translation
with gr.TabItem("Translation", id="subtab_translate"):
with gr.Group(elem_id="translate_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
translate_text = gr.Textbox(
label="Text to Translate",
placeholder="Enter text to translate...",
lines=5,
elem_id="translate_text"
)
target_lang = gr.Dropdown(
["French", "Spanish", "German", "Japanese", "Chinese", "Russian", "Arabic", "Hindi"],
label="Target Language",
value="French",
elem_id="target_lang"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="translate_params"):
translate_params = create_parameter_ui()
translate_btn = gr.Button("Translate", variant="primary", size="lg", elem_id="translate_btn")
with gr.Column(scale=1):
translation_output = gr.Textbox(
label="Translation",
lines=5,
elem_id="translation_output"
)
# Code Capabilities Tab
with gr.TabItem("Code Capabilities", id="tab_code"):
gr.Markdown(
"""
## π» Code Generation and Understanding
Test Gemma's ability to generate, explain, and debug code in various programming languages.
"""
)
with gr.Tabs() as code_tabs:
# Code Generation
with gr.TabItem("Code Generation", id="subtab_code_gen"):
with gr.Group(elem_id="code_gen_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
code_language = gr.Dropdown(
["Python", "JavaScript", "Java", "C++", "HTML/CSS", "SQL", "Bash"],
label="Programming Language",
value="Python",
elem_id="code_language"
)
code_task = gr.Textbox(
label="Coding Task",
placeholder="Describe what you want the code to do...",
value="Create a function to find prime numbers up to n",
elem_id="code_task"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="code_gen_params"):
code_gen_params = create_parameter_ui()
code_gen_btn = gr.Button("Generate Code", variant="primary", size="lg", elem_id="code_gen_btn")
with gr.Column(scale=1):
code_output = gr.Code(
label="Generated Code",
language="python",
elem_id="code_output"
)
def code_gen_handler(language, task, max_length, temperature, top_p):
language = safe_value(language, "Python")
task = safe_value(task, "write a hello world program")
prompt = generate_prompt("code_generate", language=language, task=task)
result = generate_text(prompt, max_length, temperature, top_p)
return result
# Update language in code output component
def update_code_language(lang):
lang_map = {
"Python": "python",
"JavaScript": "javascript",
"Java": "java",
"C++": "cpp",
"HTML/CSS": "html",
"SQL": "sql",
"Bash": "bash"
}
return gr.Code.update(language=lang_map.get(lang, "python"))
code_language.change(update_code_language, inputs=code_language, outputs=code_output)
code_gen_btn.click(
code_gen_handler,
inputs=[code_language, code_task, *code_gen_params],
outputs=code_output
)
# Code Explanation
with gr.TabItem("Code Explanation", id="subtab_code_explain"):
with gr.Group(elem_id="code_explain_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
code_to_explain = gr.Code(
label="Code to Explain",
language="python",
value="def quicksort(arr):\n if len(arr) <= 1:\n return arr\n pivot = arr[len(arr) // 2]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n return quicksort(left) + middle + quicksort(right)",
elem_id="code_to_explain"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="explain_code_params"):
explain_code_params = create_parameter_ui()
explain_code_btn = gr.Button("Explain Code", variant="primary", size="lg", elem_id="explain_code_btn")
with gr.Column(scale=1):
code_explanation = gr.Textbox(
label="Explanation",
lines=10,
elem_id="code_explanation"
)
def explain_code_handler(code, max_length, temperature, top_p):
code = safe_value(code, "print('Hello, world!')")
prompt = generate_prompt("code_explain", code=code)
return generate_text(prompt, max_length, temperature, top_p)
explain_code_btn.click(
explain_code_handler,
inputs=[code_to_explain, *explain_code_params],
outputs=code_explanation
)
# Code Debugging
with gr.TabItem("Code Debugging", id="subtab_code_debug"):
with gr.Group(elem_id="code_debug_box"):
with gr.Row(equal_height=True):
with gr.Column(scale=1):
code_to_debug = gr.Code(
label="Code to Debug",
language="python",
value="def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n \n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] - fib[i-2]) # Bug is here (should be +)\n \n return fib\n\nprint(fibonacci(10))",
elem_id="code_to_debug"
)
with gr.Accordion("Advanced Parameters", open=False, elem_id="debug_code_params"):
debug_code_params = create_parameter_ui()
debug_code_btn = gr.Button("Debug Code", variant="primary", size="lg", elem_id="debug_code_btn")
with gr.Column(scale=1):
debug_result = gr.Textbox(
label="Debugging Result",
lines=10,
elem_id="debug_result"
)
def debug_code_handler(code, max_length, temperature, top_p):
code = safe_value(code, "print('Hello, world!')")
prompt = generate_prompt("code_debug", code=code)
return generate_text(prompt, max_length, temperature, top_p)
debug_code_btn.click(
debug_code_handler,
inputs=[code_to_debug, *debug_code_params],
outputs=debug_result
)
# Footer
with gr.Group(elem_id="footer"):
gr.Markdown(
"""
## About Gemma
Gemma is a family of lightweight, state-of-the-art open models from Google, built from the same research and technology used to create the Gemini models.
It's designed to be efficient and accessible for various applications.
[Learn more about Gemma](https://huggingface.co/google/gemma-3-4b-pt)
<div style="text-align: center; margin-top: 20px; padding: 10px;">
<p>Β© 2023 Gemma Capabilities Demo | Made with β€οΈ using Gradio</p>
</div>
"""
)
# Add CSS for better styling
demo.load(js="""
() => {
// Add custom CSS for better styling
const style = document.createElement('style');
style.textContent = `
.tabs {
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
border-radius: 10px;
overflow: hidden;
}
/* Make buttons more noticeable */
button.primary {
transition: transform 0.2s, box-shadow 0.2s;
}
button.primary:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
/* Add hover effect to tabs */
.tab-nav button {
transition: background-color 0.3s;
}
/* Make textboxes more readable */
textarea, .input-box {
font-size: 16px !important;
}
/* Improve box styling */
.container {
border-radius: 10px;
overflow: hidden;
}
`;
document.head.appendChild(style);
}
""")
# Load default token if available
if DEFAULT_HF_TOKEN:
demo.load(lambda x: authenticate(DEFAULT_HF_TOKEN), outputs=auth_status).then(
lambda x: auth_complete(DEFAULT_HF_TOKEN), outputs=auth_status
)
demo.launch(share=False) |