pramodkoujalagi's picture
Update app.py
86bf279 verified
raw
history blame
2.23 kB
import gradio as gr
from huggingface_hub import InferenceClient
import json
# Initialize the client with your model
client = InferenceClient("pramodkoujalagi/SmolLM2-360M-Instruct-Text-2-JSON")
def respond(message, history: list[tuple[str, str]]):
# Format the prompt according to your model's expected input format
formatted_prompt = f"""<|im_start|>user
Extract the relevant event information from this text and organize it into a JSON structure with fields for action, date, time, attendees, location, duration, recurrence, and notes. If a field is not present, return null for that field.
Text: {message}
<|im_end|>
<|im_start|>assistant
"""
# Make the API call to your model
complete_response = ""
for chunk in client.text_generation(
formatted_prompt,
max_new_tokens=512,
stream=True,
temperature=0.1,
top_p=0.95,
stop_sequences=["<|im_end|>"]
):
complete_response += chunk
# Clean up the response to get just the JSON and remove end tag
cleaned_response = complete_response.strip()
# Remove the <|im_end|> tag if present
cleaned_response = cleaned_response.replace("<|im_end|>", "").strip()
try:
# Parse the JSON to validate it
json_obj = json.loads(cleaned_response)
# Return properly formatted JSON
return json.dumps(json_obj, indent=2)
except json.JSONDecodeError:
# If parsing fails, return the raw response with end tag removed
return cleaned_response
# Create the chat interface with no additional inputs
demo = gr.ChatInterface(
respond,
examples=[
"Plan an exhibition walkthrough on 15th, April 2028 at 3 PM with Harper, Grace, and Alex in the art gallery for 1 hour.",
"Schedule a meeting with the marketing team tomorrow at 2 PM in the conference room.",
"Let's do a weekly team standup every Monday at 9 AM for 30 minutes starting next week.",
"Reminder to pick up groceries this Saturday afternoon."
],
title="Calendar Event Extraction",
description="Enter text containing event information, and I'll extract the details into a JSON format."
)
if __name__ == "__main__":
demo.launch()