Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
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
|
25 |
output = model.generate(
|
26 |
encoded_input["input_ids"],
|
27 |
attention_mask = encoded_input["attention_mask"],
|
28 |
-
max_new_tokens = 200,
|
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
|
37 |
generated_text = tokenizer.decode(output.sequences[0], skip_special_tokens = True)
|
38 |
|
|
|
|
|
|
|
39 |
return generated_text
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Build the Gradio interface.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
placeholder = "Enter an excerpt from the Mahabharata here..."
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
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()
|