Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
tiiuae/"falcon-7b-instruct"
|
| 4 |
-
client = InferenceClient(model="openai/gpt-oss-20b") # No token
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 7 |
messages = [{"role": "system", "content": system_message}]
|
| 8 |
messages += history
|
|
@@ -22,19 +24,38 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 22 |
response += token
|
| 23 |
yield response
|
| 24 |
except Exception as e:
|
| 25 |
-
yield f"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
gr.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Load the model from Hugging Face Hub
|
| 5 |
+
client = InferenceClient(model="tiiuae/falcon-7b-instruct")
|
| 6 |
+
|
| 7 |
+
# Chat completion function
|
| 8 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 9 |
messages = [{"role": "system", "content": system_message}]
|
| 10 |
messages += history
|
|
|
|
| 24 |
response += token
|
| 25 |
yield response
|
| 26 |
except Exception as e:
|
| 27 |
+
yield f"[Error] {e}"
|
| 28 |
+
|
| 29 |
+
# Gradio interface layout
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
gr.Markdown("### 🧠 Falcon-7B-Instruct Chat UI — Powered by Hugging Face")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
system_message = gr.Textbox(value="You are a helpful assistant.", label="System Prompt", lines=2)
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
message = gr.Textbox(placeholder="Ask something…", label="Your Message", lines=2)
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
max_tokens = gr.Slider(minimum=64, maximum=1024, value=256, step=64, label="Max Tokens")
|
| 41 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
|
| 42 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Top-p (nucleus sampling)")
|
| 43 |
+
|
| 44 |
+
chatbot = gr.Chatbot()
|
| 45 |
+
state = gr.State([])
|
| 46 |
+
|
| 47 |
+
submit = gr.Button("Send")
|
| 48 |
+
|
| 49 |
+
def handle_submit(user_message, history, system_message, max_tokens, temperature, top_p):
|
| 50 |
+
history = history + [[user_message, ""]]
|
| 51 |
+
for updated_response in respond(user_message, history[:-1], system_message, max_tokens, temperature, top_p):
|
| 52 |
+
history[-1][1] = updated_response
|
| 53 |
+
yield history, history
|
| 54 |
+
|
| 55 |
+
submit.click(
|
| 56 |
+
handle_submit,
|
| 57 |
+
inputs=[message, state, system_message, max_tokens, temperature, top_p],
|
| 58 |
+
outputs=[chatbot, state],
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
demo.launch()
|