Spaces:
Sleeping
Sleeping
Commit ·
e88fbe9
1
Parent(s): bd0acf8
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
| 7 |
|
| 8 |
def generate_dialogue(topic):
|
| 9 |
# System message to instruct the model
|
| 10 |
-
system_message = "Generate a
|
| 11 |
|
| 12 |
# Conversation setup
|
| 13 |
messages = [
|
|
@@ -16,48 +16,38 @@ def generate_dialogue(topic):
|
|
| 16 |
]
|
| 17 |
|
| 18 |
response = ""
|
| 19 |
-
last_sentence = ""
|
| 20 |
|
| 21 |
-
# Generate the dialogue
|
|
|
|
|
|
|
| 22 |
for msg in client.chat_completion(
|
| 23 |
messages,
|
| 24 |
-
max_tokens=
|
| 25 |
stream=True,
|
| 26 |
temperature=0.7,
|
| 27 |
top_p=0.95,
|
| 28 |
):
|
| 29 |
-
token = msg.choices[0].delta.content
|
| 30 |
-
|
| 31 |
-
# Ensure correct spacing between words and punctuation
|
| 32 |
-
if response and not response.endswith((" ", "\n")) and not token.startswith((" ", "\n", ".", "?", "!", ",")):
|
| 33 |
-
response += " "
|
| 34 |
-
|
| 35 |
response += token
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
if
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
yield response # Stream output
|
| 42 |
-
|
| 43 |
-
#time.sleep(0.1) # Slow down slightly for smoother streaming
|
| 44 |
-
|
| 45 |
-
# Ensure the dialogue ends at a complete sentence
|
| 46 |
-
if response and response[-1] not in ".!?":
|
| 47 |
-
response = last_sentence
|
| 48 |
-
|
| 49 |
-
yield response # Final output
|
| 50 |
|
| 51 |
# Define the Gradio UI
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
gr.Markdown("## 🎭 AI Dialogue Generator")
|
| 54 |
-
gr.Markdown("Enter a topic, and the AI will generate a
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
topic_input = gr.Textbox(label="Enter a topic", placeholder="e.g., Space Travel, AI in Healthcare")
|
| 58 |
generate_btn = gr.Button("Generate Dialogue")
|
| 59 |
|
| 60 |
-
output_box = gr.
|
| 61 |
|
| 62 |
generate_btn.click(generate_dialogue, inputs=topic_input, outputs=output_box)
|
| 63 |
|
|
|
|
| 7 |
|
| 8 |
def generate_dialogue(topic):
|
| 9 |
# System message to instruct the model
|
| 10 |
+
system_message = "Generate a short and engaging dialogue between two people on the given topic."
|
| 11 |
|
| 12 |
# Conversation setup
|
| 13 |
messages = [
|
|
|
|
| 16 |
]
|
| 17 |
|
| 18 |
response = ""
|
|
|
|
| 19 |
|
| 20 |
+
# Generate the dialogue with streaming and delay
|
| 21 |
+
start_time = time.time()
|
| 22 |
+
|
| 23 |
for msg in client.chat_completion(
|
| 24 |
messages,
|
| 25 |
+
max_tokens=250, # Enough tokens for a dialogue
|
| 26 |
stream=True,
|
| 27 |
temperature=0.7,
|
| 28 |
top_p=0.95,
|
| 29 |
):
|
| 30 |
+
token = msg.choices[0].delta.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
response += token
|
| 32 |
+
|
| 33 |
+
# Yield response to stream the output progressively
|
| 34 |
+
yield response.strip()
|
| 35 |
|
| 36 |
+
# # If the dialogue is complete, stop after 10 seconds
|
| 37 |
+
# if time.time() - start_time >= 10:
|
| 38 |
+
# break
|
| 39 |
+
# time.sleep(0.2) # Slows token output for smoother streaming
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Define the Gradio UI
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("## 🎭 AI Dialogue Generator")
|
| 44 |
+
gr.Markdown("Enter a topic, and the AI will generate a short dialogue between two people.")
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
topic_input = gr.Textbox(label="Enter a topic", placeholder="e.g., Space Travel, AI in Healthcare")
|
| 48 |
generate_btn = gr.Button("Generate Dialogue")
|
| 49 |
|
| 50 |
+
output_box = gr.Textbox(label="Generated Dialogue", interactive=False, lines=10)
|
| 51 |
|
| 52 |
generate_btn.click(generate_dialogue, inputs=topic_input, outputs=output_box)
|
| 53 |
|