Samurai719214 commited on
Commit
f004663
·
verified ·
1 Parent(s): 8cfe65c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -21
app.py CHANGED
@@ -11,21 +11,25 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  model.to(device)
13
 
 
 
 
14
  def generate_full_story(excerpt: str) -> str:
15
  """
16
- Given an incomplete story excerpt (without header details), this function calls the model
17
- to generate the complete story that includes Parv, Key Event, Section and the story continuation.
18
  """
 
 
 
19
  # Tokenize the user-provided excerpt.
20
  encoded_input = tokenizer(excerpt, return_tensors = "pt")
21
- # Move tensors to the appropriate device.
22
  encoded_input = {k: v.to(device) for k, v in encoded_input.items()}
23
 
24
- # Generate tokens. Here, we set parameters to control length and creativity.
25
  output = model.generate(
26
  encoded_input["input_ids"],
27
  attention_mask = encoded_input["attention_mask"],
28
- max_new_tokens = 200, # Generate 200 new tokens on top of the input.
29
  do_sample = True,
30
  temperature = 0.8,
31
  top_p = 0.95,
@@ -33,26 +37,46 @@ def generate_full_story(excerpt: str) -> str:
33
  return_dict_in_generate = True
34
  )
35
 
36
- # Decode the generated sequence.
37
  generated_text = tokenizer.decode(output.sequences[0], skip_special_tokens = True)
38
 
 
 
 
39
  return generated_text
40
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Build the Gradio interface.
42
- interface = gr.Interface(
43
- fn = generate_full_story,
44
- inputs = gr.Textbox(
45
- lines = 5,
46
- label = "Incomplete story excerpt",
47
- placeholder = "Enter an excerpt from the Mahabharata here..."
48
- ),
49
- outputs = gr.Textbox(label = "Chapter summary"),
50
- title = "🏺 Mythology Storyteller",
51
- description = (
52
- "Enter a phrase from a chapter of your choice (if possible please enter the Parv, Key Event, & Section for an accurate answer). "
53
- "The model will generate the summary of the respective chapter."
54
- )
55
- )
 
 
 
 
 
 
56
 
57
  # Launch the Gradio app.
58
- interface.launch()
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  model.to(device)
13
 
14
+ # Store conversation history
15
+ conversation_history = []
16
+
17
  def generate_full_story(excerpt: str) -> str:
18
  """
19
+ Given an incomplete story excerpt, generate the complete story including Parv, Key Event, Section, and continuation.
 
20
  """
21
+ if not excerpt.strip():
22
+ return "Please enter a valid story excerpt."
23
+
24
  # Tokenize the user-provided excerpt.
25
  encoded_input = tokenizer(excerpt, return_tensors = "pt")
 
26
  encoded_input = {k: v.to(device) for k, v in encoded_input.items()}
27
 
28
+ # Generate text with controlled parameters.
29
  output = model.generate(
30
  encoded_input["input_ids"],
31
  attention_mask = encoded_input["attention_mask"],
32
+ max_new_tokens = 200,
33
  do_sample = True,
34
  temperature = 0.8,
35
  top_p = 0.95,
 
37
  return_dict_in_generate = True
38
  )
39
 
40
+ # Decode generated text.
41
  generated_text = tokenizer.decode(output.sequences[0], skip_special_tokens = True)
42
 
43
+ # Append to conversation history
44
+ conversation_history.append((excerpt, generated_text))
45
+
46
  return generated_text
47
 
48
+ def get_conversation_history():
49
+ """Displays conversation history."""
50
+ if not conversation_history:
51
+ return "No conversations started."
52
+ return "\n\n".join([f"**User:** {inp}\n**AI:** {out}" for inp, out in conversation_history])
53
+
54
+ def clear_conversation():
55
+ """Clears the conversation history."""
56
+ conversation_history.clear()
57
+ return "No conversations started."
58
+
59
  # Build the Gradio interface.
60
+ with gr.Blocks() as interface:
61
+ gr.Markdown("# 🏺 Mythology Storyteller")
62
+ gr.Markdown("Enter a phrase from a chapter of your choice. The model will generate the summary of the respective chapter.")
63
+
64
+ with gr.Row():
65
+ user_input = gr.Textbox(lines = 5, label = "Incomplete story excerpt", placeholder = "Enter an excerpt from the Mahabharata here...")
66
+
67
+ output_text = gr.Textbox(label = "Chapter summary")
68
+
69
+ generate_btn = gr.Button("Generate Story")
70
+ generate_btn.click(fn = generate_full_story, inputs = user_input, outputs = output_text)
71
+
72
+ with gr.Row():
73
+ history_display = gr.Textbox(label = "Conversation History", interactive = False)
74
+
75
+ show_history_btn = gr.Button("Show Conversation History")
76
+ show_history_btn.click(fn = get_conversation_history, outputs = history_display)
77
+
78
+ clear_btn = gr.Button("Clear Conversation")
79
+ clear_btn.click(fn = clear_conversation, outputs = history_display)
80
 
81
  # Launch the Gradio app.
82
+ interface.launch()