Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,33 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
* {
|
| 11 |
-
font-family: 'JetBrains Mono', monospace !important;
|
| 12 |
-
}
|
| 13 |
|
|
|
|
| 14 |
body {
|
|
|
|
| 15 |
background-color: #111;
|
| 16 |
color: #e0e0e0;
|
| 17 |
}
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
.markdown-think {
|
| 20 |
background-color: #1e1e1e;
|
| 21 |
border-left: 4px solid #555;
|
| 22 |
padding: 10px;
|
| 23 |
margin-bottom: 8px;
|
| 24 |
font-style: italic;
|
| 25 |
-
white-space: pre-wrap;
|
| 26 |
animation: pulse 1.5s infinite ease-in-out;
|
| 27 |
}
|
| 28 |
-
|
| 29 |
@keyframes pulse {
|
| 30 |
0% { opacity: 0.6; }
|
| 31 |
50% { opacity: 1.0; }
|
|
@@ -33,6 +36,7 @@ body {
|
|
| 33 |
}
|
| 34 |
"""
|
| 35 |
|
|
|
|
| 36 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 37 |
messages = [{"role": "system", "content": system_message}] if system_message else []
|
| 38 |
|
|
@@ -42,15 +46,14 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 42 |
if assistant:
|
| 43 |
messages.append({"role": "assistant", "content": assistant})
|
| 44 |
|
| 45 |
-
thinking_prompt = messages + [
|
| 46 |
-
"role": "user",
|
| 47 |
-
|
| 48 |
-
}]
|
| 49 |
|
| 50 |
reasoning = ""
|
| 51 |
yield '<div class="markdown-think">Thinking...</div>'
|
| 52 |
|
| 53 |
-
for chunk in
|
| 54 |
thinking_prompt,
|
| 55 |
max_tokens=max_tokens,
|
| 56 |
stream=True,
|
|
@@ -59,8 +62,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 59 |
):
|
| 60 |
token = chunk.choices[0].delta.content or ""
|
| 61 |
reasoning += token
|
| 62 |
-
|
| 63 |
-
yield styled_thought
|
| 64 |
|
| 65 |
time.sleep(0.5)
|
| 66 |
|
|
@@ -71,7 +73,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 71 |
]
|
| 72 |
|
| 73 |
final_answer = ""
|
| 74 |
-
for chunk in
|
| 75 |
final_prompt,
|
| 76 |
max_tokens=max_tokens,
|
| 77 |
stream=True,
|
|
@@ -82,19 +84,35 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 82 |
final_answer += token
|
| 83 |
yield final_answer.strip()
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
gr.
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import time
|
| 4 |
|
| 5 |
+
# Clientes para texto e imagem
|
| 6 |
+
chat_client = InferenceClient("lambdaindie/lambdai")
|
| 7 |
+
image_client = InferenceClient("stabilityai/stable-diffusion-2")
|
| 8 |
|
| 9 |
+
# Fonte global
|
| 10 |
+
gr.themes.Base().set(font=["JetBrains Mono", "monospace"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
css = """
|
| 13 |
body {
|
| 14 |
+
font-family: 'JetBrains Mono', monospace;
|
| 15 |
background-color: #111;
|
| 16 |
color: #e0e0e0;
|
| 17 |
}
|
| 18 |
+
.gr-textbox textarea {
|
| 19 |
+
background-color: #181818 !important;
|
| 20 |
+
color: #fff !important;
|
| 21 |
+
font-family: 'JetBrains Mono', monospace;
|
| 22 |
+
border-radius: 8px;
|
| 23 |
+
}
|
| 24 |
.markdown-think {
|
| 25 |
background-color: #1e1e1e;
|
| 26 |
border-left: 4px solid #555;
|
| 27 |
padding: 10px;
|
| 28 |
margin-bottom: 8px;
|
| 29 |
font-style: italic;
|
|
|
|
| 30 |
animation: pulse 1.5s infinite ease-in-out;
|
| 31 |
}
|
|
|
|
| 32 |
@keyframes pulse {
|
| 33 |
0% { opacity: 0.6; }
|
| 34 |
50% { opacity: 1.0; }
|
|
|
|
| 36 |
}
|
| 37 |
"""
|
| 38 |
|
| 39 |
+
# Função do chatbot com raciocínio
|
| 40 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 41 |
messages = [{"role": "system", "content": system_message}] if system_message else []
|
| 42 |
|
|
|
|
| 46 |
if assistant:
|
| 47 |
messages.append({"role": "assistant", "content": assistant})
|
| 48 |
|
| 49 |
+
thinking_prompt = messages + [
|
| 50 |
+
{"role": "user", "content": f"{message}\n\nThink step-by-step before answering."}
|
| 51 |
+
]
|
|
|
|
| 52 |
|
| 53 |
reasoning = ""
|
| 54 |
yield '<div class="markdown-think">Thinking...</div>'
|
| 55 |
|
| 56 |
+
for chunk in chat_client.chat_completion(
|
| 57 |
thinking_prompt,
|
| 58 |
max_tokens=max_tokens,
|
| 59 |
stream=True,
|
|
|
|
| 62 |
):
|
| 63 |
token = chunk.choices[0].delta.content or ""
|
| 64 |
reasoning += token
|
| 65 |
+
yield f'<div class="markdown-think">{reasoning.strip()}</div>'
|
|
|
|
| 66 |
|
| 67 |
time.sleep(0.5)
|
| 68 |
|
|
|
|
| 73 |
]
|
| 74 |
|
| 75 |
final_answer = ""
|
| 76 |
+
for chunk in chat_client.chat_completion(
|
| 77 |
final_prompt,
|
| 78 |
max_tokens=max_tokens,
|
| 79 |
stream=True,
|
|
|
|
| 84 |
final_answer += token
|
| 85 |
yield final_answer.strip()
|
| 86 |
|
| 87 |
+
# Função para gerar imagem
|
| 88 |
+
def generate_image(prompt):
|
| 89 |
+
return image_client.text_to_image(prompt, guidance_scale=7.5)
|
| 90 |
+
|
| 91 |
+
# Interface Gradio
|
| 92 |
+
with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
| 93 |
+
gr.Markdown("# λmabdAI")
|
| 94 |
+
|
| 95 |
+
with gr.Tabs():
|
| 96 |
+
with gr.Tab("Chat"):
|
| 97 |
+
gr.ChatInterface(
|
| 98 |
+
fn=respond,
|
| 99 |
+
additional_inputs=[
|
| 100 |
+
gr.Textbox(
|
| 101 |
+
value="You are a concise, logical AI that explains its reasoning clearly before answering.",
|
| 102 |
+
label="System Message"
|
| 103 |
+
),
|
| 104 |
+
gr.Slider(64, 2048, value=512, step=1, label="Max Tokens"),
|
| 105 |
+
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 106 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 107 |
+
]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with gr.Tab("Image Generator"):
|
| 111 |
+
gr.Markdown("### Generate an image from a prompt")
|
| 112 |
+
prompt = gr.Textbox(label="Prompt")
|
| 113 |
+
output = gr.Image(type="pil")
|
| 114 |
+
btn = gr.Button("Generate")
|
| 115 |
+
btn.click(fn=generate_image, inputs=prompt, outputs=output)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo.launch()
|