Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
from http import HTTPStatus | |
import dashscope | |
from transformers import pipeline | |
from typing import List, Optional, Tuple, Dict | |
from urllib.error import HTTPError | |
default_system = 'You are a helpful assistant.' | |
History = List[Tuple[str, str]] | |
Messages = List[Dict[str, str]] | |
def clear_session() -> History: | |
return '', [] | |
def modify_system_session(system: str) -> str: | |
if system is None or len(system) == 0: | |
system = default_system | |
return system, system, [] | |
def history_to_messages(history: History, system: str) -> Messages: | |
messages = [{'role': Role.SYSTEM, 'content': system}] | |
for h in history: | |
messages.append({'role': Role.USER, 'content': h[0]}) | |
messages.append({'role': Role.ASSISTANT, 'content': h[1]}) | |
return messages | |
def messages_to_history(messages: Messages) -> Tuple[str, History]: | |
assert messages[0]['role'] == Role.SYSTEM | |
system = messages[0]['content'] | |
history = [] | |
for q, r in zip(messages[1::2], messages[2::2]): | |
history.append([q['content'], r['content']]) | |
return system, history | |
def model_chat(query: Optional[str], history: Optional[History], system: str | |
) -> Tuple[str, str, History]: | |
if query is None: | |
query = '' | |
if history is None: | |
history = [] | |
messages = history_to_messages(history, system) | |
messages.append({'role': Role.USER, 'content': query}) | |
generator = pipeline('text-generation', model='microsoft/Phi-3-mini-128k-instruct') | |
response = generator(query, max_length=150) # Ajusta la longitud máxima según necesidad | |
role = Role.ASSISTANT | |
response_content = response[0]['generated_text'] | |
system, history = messages_to_history(messages + [{'role': role, 'content': response_content}]) | |
return '', history, system | |
with gr.Blocks() as demo: | |
with gr.TabBar(): | |
with gr.Tab("Model Info"): | |
gr.Markdown("""Modelo actual: `microsoft/Phi-3-mini-128k-instruct`""") | |
with gr.Tab("Chat"): | |
gr.Markdown("""<center><font size=8>Chat Bot Preview👾</center>""") | |
with gr.Row(): | |
with gr.Column(scale=3): | |
system_input = gr.Textbox(value=default_system, lines=1, label='System') | |
with gr.Column(scale=1): | |
modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2) | |
system_state = gr.Textbox(value=default_system, visible=False) | |
chatbot = gr.Chatbot(label='Chat with AI') | |
textbox = gr.Textbox(lines=2, label='Input') | |
with gr.Row(): | |
clear_history = gr.Button("🧹 Clear history") | |
submit = gr.Button("🚀 Send") | |
submit.click(model_chat, | |
inputs=[textbox, chatbot, system_state], | |
outputs=[textbox, chatbot, system_input], | |
concurrency_limit=100) | |
clear_history.click(fn=clear_session, | |
inputs=[], | |
outputs=[textbox, chatbot]) | |
modify_system.click(fn=modify_system_session, | |
inputs=[system_input], | |
outputs=[system_state, system_input, chatbot]) | |
demo.queue(api_open=False) | |
demo.launch(max_threads=30) | |