Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Public model URL - no API key needed
|
| 5 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 6 |
+
|
| 7 |
+
def query_hf_model(messages):
|
| 8 |
+
# Convert Gradio format (list of {"role":..., "content":...}) to prompt text
|
| 9 |
+
prompt = "\n".join(
|
| 10 |
+
f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages if msg["role"] in {"user", "assistant"}
|
| 11 |
+
) + "\nAssistant:"
|
| 12 |
+
|
| 13 |
+
payload = {
|
| 14 |
+
"inputs": prompt,
|
| 15 |
+
"parameters": {
|
| 16 |
+
"max_new_tokens": 128,
|
| 17 |
+
"temperature": 0.7,
|
| 18 |
+
"return_full_text": True
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
response = requests.post(HF_API_URL, json=payload)
|
| 23 |
+
if response.status_code == 200:
|
| 24 |
+
result = response.json()[0]["generated_text"]
|
| 25 |
+
reply = result[len(prompt):].strip()
|
| 26 |
+
return reply
|
| 27 |
+
else:
|
| 28 |
+
return f"⚠️ Error {response.status_code}: {response.reason}"
|
| 29 |
+
|
| 30 |
+
def respond(user_input, chat_history):
|
| 31 |
+
# Add user message to history
|
| 32 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 33 |
+
reply = query_hf_model(chat_history)
|
| 34 |
+
chat_history.append({"role": "assistant", "content": reply})
|
| 35 |
+
return "", chat_history
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("### 🤖 Mistral Chatbot — No Auth Required")
|
| 39 |
+
|
| 40 |
+
chatbot = gr.Chatbot(label="Chat with AI", type="messages", avatar_images=("👤", "🤖"))
|
| 41 |
+
msg = gr.Textbox(label="Your message", placeholder="Ask me anything...", scale=1)
|
| 42 |
+
|
| 43 |
+
msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 44 |
+
|
| 45 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|