Nicolás Larenas
commited on
Update gradio_interface.py
Browse files- gradio_interface.py +15 -11
gradio_interface.py
CHANGED
|
@@ -4,7 +4,6 @@ import gradio as gr
|
|
| 4 |
from ai_model import query_ai_model, preprocess_chat_history
|
| 5 |
import asyncio
|
| 6 |
import logging
|
| 7 |
-
from typing import List, Optional
|
| 8 |
from config import (
|
| 9 |
DEFAULT_MAX_OUTPUT_TOKENS,
|
| 10 |
DEFAULT_TEMPERATURE,
|
|
@@ -12,6 +11,8 @@ from config import (
|
|
| 12 |
DEFAULT_TOP_K,
|
| 13 |
)
|
| 14 |
import os
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Configure logging
|
| 17 |
logging.basicConfig(level=logging.ERROR)
|
|
@@ -46,36 +47,39 @@ async def bot(
|
|
| 46 |
return
|
| 47 |
|
| 48 |
genai.configure(api_key=google_key)
|
|
|
|
| 49 |
stop_sequences_list = preprocess_stop_sequences(stop_sequences)
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
history = chatbot[:-1] # Exclude the last
|
| 55 |
|
| 56 |
# Query the AI model
|
| 57 |
assistant_reply = await query_ai_model(
|
| 58 |
-
message=
|
| 59 |
history=history,
|
| 60 |
max_output_tokens=max_output_tokens,
|
| 61 |
temperature=temperature,
|
| 62 |
top_p=top_p,
|
| 63 |
top_k=top_k,
|
| 64 |
-
stop_sequences=stop_sequences_list
|
| 65 |
)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
chatbot[-1] = (chatbot[-1][0], "")
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
chatbot[-1] = (chatbot[-1][0], chatbot[-1][1] + section)
|
| 74 |
await asyncio.sleep(0.01)
|
| 75 |
yield chatbot
|
| 76 |
except Exception as e:
|
| 77 |
logging.error("Error in bot function", exc_info=True)
|
| 78 |
-
|
|
|
|
| 79 |
yield chatbot
|
| 80 |
|
| 81 |
def create_gradio_interface():
|
|
|
|
| 4 |
from ai_model import query_ai_model, preprocess_chat_history
|
| 5 |
import asyncio
|
| 6 |
import logging
|
|
|
|
| 7 |
from config import (
|
| 8 |
DEFAULT_MAX_OUTPUT_TOKENS,
|
| 9 |
DEFAULT_TEMPERATURE,
|
|
|
|
| 11 |
DEFAULT_TOP_K,
|
| 12 |
)
|
| 13 |
import os
|
| 14 |
+
from typing import List, Optional # <-- Added this import
|
| 15 |
+
import time
|
| 16 |
|
| 17 |
# Configure logging
|
| 18 |
logging.basicConfig(level=logging.ERROR)
|
|
|
|
| 47 |
return
|
| 48 |
|
| 49 |
genai.configure(api_key=google_key)
|
| 50 |
+
|
| 51 |
stop_sequences_list = preprocess_stop_sequences(stop_sequences)
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
# Process the latest user message
|
| 55 |
+
message = chatbot[-1][0]
|
| 56 |
+
history = preprocess_chat_history(chatbot[:-1]) # Exclude the last message
|
| 57 |
|
| 58 |
# Query the AI model
|
| 59 |
assistant_reply = await query_ai_model(
|
| 60 |
+
message=message,
|
| 61 |
history=history,
|
| 62 |
max_output_tokens=max_output_tokens,
|
| 63 |
temperature=temperature,
|
| 64 |
top_p=top_p,
|
| 65 |
top_k=top_k,
|
| 66 |
+
stop_sequences=stop_sequences_list
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Update the chat history with the assistant's reply
|
| 70 |
chatbot[-1] = (chatbot[-1][0], "")
|
| 71 |
+
response_content = assistant_reply['content']
|
| 72 |
|
| 73 |
+
# Streaming effect
|
| 74 |
+
for i in range(0, len(response_content), 10):
|
| 75 |
+
section = response_content[i:i + 10]
|
| 76 |
chatbot[-1] = (chatbot[-1][0], chatbot[-1][1] + section)
|
| 77 |
await asyncio.sleep(0.01)
|
| 78 |
yield chatbot
|
| 79 |
except Exception as e:
|
| 80 |
logging.error("Error in bot function", exc_info=True)
|
| 81 |
+
error_message = f"An error occurred: {str(e)}"
|
| 82 |
+
chatbot[-1] = (chatbot[-1][0], error_message)
|
| 83 |
yield chatbot
|
| 84 |
|
| 85 |
def create_gradio_interface():
|