IotESP / app.py
AhaAI's picture
Create app.py
56261ce verified
Raw History Blame Contribute Delete
937 Bytes
import gradio as gr
from transformers import pipeline
import time
# Use a smaller model if HF space has <1GB RAM
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")
def chat_with_ai(prompt):
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if not prompt:
return f"[{timestamp}] Error: No prompt received."
try:
response = generator(prompt, max_new_tokens=50)[0]['generated_text']
return f"[{timestamp}] {response}"
except Exception as e:
return f"[{timestamp}] Internal Error: {str(e)}"
iface = gr.Interface(
fn=chat_with_ai,
inputs=gr.Textbox(label="Prompt"),
outputs=gr.Textbox(label="Response"),
title="ESP8266 Web AI",
live=True,
description="AI API endpoint for microcontrollers. Send POST to /api/predict."
)
app = gr.mount_gradio_app(app=iface.launch(share=False, server_name="0.0.0.0", server_port=7860), path="/")