Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,135 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
+
# Minimal Configuration
|
| 5 |
+
API_URL = "http://127.0.0.1:8080/v1/chat/completions"
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Store the last prompt
|
| 8 |
+
last_prompt = ""
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def generate(prompt):
|
| 12 |
+
"""Generation function with prompt storage"""
|
| 13 |
+
global last_prompt
|
| 14 |
+
last_prompt = prompt
|
| 15 |
+
try:
|
| 16 |
+
response = requests.post(
|
| 17 |
+
API_URL,
|
| 18 |
+
json={
|
| 19 |
+
"model": "TinyLlama-1.1B-Chat-v1.0",
|
| 20 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 21 |
+
"max_tokens": 256
|
| 22 |
+
},
|
| 23 |
+
timeout=100
|
| 24 |
+
)
|
| 25 |
+
return response.json()['choices'][0]['message']['content']
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Error: {str(e)}"
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
+
def regenerate():
|
| 31 |
+
"""Regenerate response using last prompt"""
|
| 32 |
+
if last_prompt:
|
| 33 |
+
return generate(last_prompt)
|
| 34 |
+
return "No previous prompt to regenerate"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Styled UI with improved organization
|
| 38 |
+
with gr.Blocks(title="Karlson Achegeba GPT", theme=gr.themes.Soft(primary_hue="blue")) as app:
|
| 39 |
+
with gr.Column(elem_classes=["center-container"]):
|
| 40 |
+
# Header Section
|
| 41 |
+
gr.Markdown(
|
| 42 |
+
"""<div style='text-align: center;'>
|
| 43 |
+
<h1 style='color: #2563eb; margin-bottom: 0;'>Karlson Achegeba GPT</h1>
|
| 44 |
+
<p style='color: #64748b;'>Powered by TinyLlama</p>
|
| 45 |
+
</div>"""
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
# Chat Interface
|
| 49 |
+
with gr.Group(elem_classes=["chat-container"]):
|
| 50 |
+
# Input Section
|
| 51 |
+
with gr.Column():
|
| 52 |
+
prompt = gr.Textbox(
|
| 53 |
+
placeholder="Type your message here...",
|
| 54 |
+
lines=5,
|
| 55 |
+
label="Your Message",
|
| 56 |
+
elem_classes=["input-box"]
|
| 57 |
+
)
|
| 58 |
|
| 59 |
+
# Action Buttons
|
| 60 |
+
with gr.Row():
|
| 61 |
+
submit = gr.Button(
|
| 62 |
+
"Generate Response",
|
| 63 |
+
variant="primary",
|
| 64 |
+
elem_classes=["action-button"]
|
| 65 |
+
)
|
| 66 |
+
regenerate_btn = gr.Button(
|
| 67 |
+
"Regenerate",
|
| 68 |
+
variant="secondary",
|
| 69 |
+
elem_classes=["action-button"]
|
| 70 |
+
)
|
| 71 |
+
clear_btn = gr.Button(
|
| 72 |
+
"Clear",
|
| 73 |
+
variant="secondary",
|
| 74 |
+
elem_classes=["action-button"]
|
| 75 |
+
)
|
|
|
|
| 76 |
|
| 77 |
+
# Output Section
|
| 78 |
+
output = gr.Textbox(
|
| 79 |
+
label="AI Response",
|
| 80 |
+
interactive=False,
|
| 81 |
+
lines=10,
|
| 82 |
+
elem_classes=["output-box"]
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
# Event Handlers
|
| 86 |
+
submit.click(fn=generate, inputs=prompt, outputs=output)
|
| 87 |
+
regenerate_btn.click(fn=regenerate, outputs=output)
|
| 88 |
+
clear_btn.click(fn=lambda: ("", ""), outputs=[prompt, output]) # Clears both input and output
|
| 89 |
+
|
| 90 |
+
# Custom CSS
|
| 91 |
+
app.css = """
|
| 92 |
+
.center-container {
|
| 93 |
+
max-width: 800px;
|
| 94 |
+
margin: auto;
|
| 95 |
+
padding: 20px;
|
| 96 |
+
}
|
| 97 |
+
.chat-container {
|
| 98 |
+
display: flex;
|
| 99 |
+
flex-direction: column;
|
| 100 |
+
gap: 20px;
|
| 101 |
+
}
|
| 102 |
+
.input-box, .output-box {
|
| 103 |
+
border-radius: 8px;
|
| 104 |
+
border: 1px solid #e2e8f0;
|
| 105 |
+
padding: 12px;
|
| 106 |
+
}
|
| 107 |
+
.input-box textarea, .output-box textarea {
|
| 108 |
+
font-size: 16px !important;
|
| 109 |
+
}
|
| 110 |
+
.action-button {
|
| 111 |
+
border-radius: 8px !important;
|
| 112 |
+
padding: 8px 16px !important;
|
| 113 |
+
min-width: 120px;
|
| 114 |
+
}
|
| 115 |
+
.action-button:not(.secondary) {
|
| 116 |
+
background: #2563eb !important;
|
| 117 |
+
color: white !important;
|
| 118 |
+
}
|
| 119 |
+
.action-button.secondary {
|
| 120 |
+
border: 1px solid #2563eb !important;
|
| 121 |
+
color: #2563eb !important;
|
| 122 |
+
}
|
| 123 |
+
.group {
|
| 124 |
+
border: 1px solid #e2e8f0;
|
| 125 |
+
border-radius: 12px;
|
| 126 |
+
padding: 20px;
|
| 127 |
+
background: white;
|
| 128 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
| 129 |
+
}
|
| 130 |
+
.row {
|
| 131 |
+
gap: 10px;
|
| 132 |
+
}
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
app.launch(server_port=7860 , share = True)
|