File size: 2,231 Bytes
a89ff2e
 
a0c7f72
a89ff2e
a0c7f72
 
a89ff2e
a0c7f72
 
 
 
a89ff2e
a0c7f72
 
 
 
 
 
 
 
 
 
a89ff2e
a0c7f72
 
 
a89ff2e
a0c7f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a89ff2e
 
a0c7f72
86bf279
 
 
 
a89ff2e
a0c7f72
 
a89ff2e
 
 
a0c7f72
1
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
47
48
49
50
51
52
53
54
55
56
57
58
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()