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