Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async and streaming #8

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import argparse
import asyncio
import gradio as gr
from difflib import Differ
from string import Template
Expand All @@ -25,7 +26,7 @@ def find_attached_file(filename, attached_files):
return file
return None

def echo(message, history, state):
async def echo(message, history, state):
attached_file = None

if message['files']:
Expand All @@ -34,7 +35,7 @@ def echo(message, history, state):

attached_file = find_attached_file(filename, state["attached_files"])
if attached_file is None:
path_gcp = client.files.upload(path=path_local)
path_gcp = await client.files.AsyncFiles.upload(path=path_local)
state["attached_files"].append({
"name": filename,
"path_local": path_local,
Expand All @@ -52,35 +53,42 @@ def echo(message, history, state):
chat_history = chat_history + user_message
state['messages'] = chat_history

response = client.models.generate_content(
model="gemini-1.5-flash",
contents=state['messages'],
)
model_response = response.text

response_chunks = ""
async for chunk in await client.aio.models.generate_content_stream(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using a variable for await client.aio.models.generate_content_stream().

model="gemini-2.0-flash", contents=state['messages'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this configurable.

):
response_chunks += chunk.text
# when model generates too fast, Gradio does not respond that in real-time.
await asyncio.sleep(0.1)
yield (
response_chunks,
state,
state['summary_diff_history'][-1] if len(state['summary_diff_history']) > 1 else "",
state['summary_history'][-1] if len(state['summary_history']) > 1 else "",
gr.Slider(
visible=False if len(state['summary_history']) <= 1 else True,
interactive=False if len(state['summary_history']) <= 1 else True,
),
)

# make summary
if state['summary'] != "":
response = client.models.generate_content(
model="gemini-1.5-flash",
contents=[
Template(
prompt_tmpl['summarization']['prompt']
).safe_substitute(
previous_summary=state['summary'],
latest_conversation=str({"user": message['text'], "assistant": model_response})
)
],
config={'response_mime_type': 'application/json',
'response_schema': SummaryResponses,
},
)
response = await client.aio.models.generate_content(
model="gemini-2.0-flash",
contents=[
Template(
prompt_tmpl['summarization']['prompt']
).safe_substitute(
previous_summary=state['summary'],
latest_conversation=str({"user": message['text'], "assistant": response_chunks})
)
],
config={'response_mime_type': 'application/json',
'response_schema': SummaryResponses,
},
)

if state['summary'] != "":
prev_summary = state['summary_history'][-1]
else:
prev_summary = ""
prev_summary = state['summary_history'][-1] if len(state['summary_history']) >= 1 else ""

d = Differ()
state['summary'] = (
response.parsed.summary
if getattr(response.parsed, "summary", None) is not None
Expand All @@ -94,14 +102,13 @@ def echo(message, history, state):
state['summary_diff_history'].append(
[
(token[2:], token[0] if token[0] != " " else None)
for token in d.compare(prev_summary, state['summary'])
for token in Differ().compare(prev_summary, state['summary'])
]
)

return (
model_response,
yield (
response_chunks,
state,
# state['summary'],
state['summary_diff_history'][-1],
state['summary_history'][-1],
gr.Slider(
Expand Down Expand Up @@ -166,7 +173,7 @@ def main(args):
# value="No summary yet. As you chat with the assistant, the summary will be updated automatically.",
combine_adjacent=True,
show_legend=True,
color_map={"+": "red", "-": "green"},
color_map={"-": "red", "+": "green"},
elem_classes=["summary-window"],
visible=False
)
Expand Down