File size: 5,932 Bytes
d6aca47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

# Crear el código completo para un espacio de Hugging Face que genera títulos
# usando Llama-3.2-1B-Instruct con interfaz Gradio y API

app_code = '''import gradio as gr
import os
from huggingface_hub import InferenceClient

# Obtener el token de HF desde los secrets
HF_TOKEN = os.environ.get("HF_TOKEN")

# Inicializar el cliente de inferencia con el modelo Llama
client = InferenceClient(
    model="meta-llama/Llama-3.2-1B-Instruct",
    token=HF_TOKEN
)

def generate_title(text_or_history, max_length=50):
    """
    Genera un título a partir de texto o historial de conversación
    
    Args:
        text_or_history: Puede ser texto simple o una lista de mensajes
        max_length: Longitud máxima del título
    
    Returns:
        El título generado
    """
    try:
        # Si es una lista (historial), convertirla a texto
        if isinstance(text_or_history, list):
            # Formatear el historial como conversación
            conversation_text = "\\n".join([
                f"{msg.get('role', 'user')}: {msg.get('content', '')}" 
                for msg in text_or_history
            ])
        else:
            conversation_text = str(text_or_history)
        
        # Crear el prompt para generar título
        prompt = f"""Based on the following conversation or text, generate a short, concise title (maximum 10 words):

{conversation_text}

Title:"""
        
        # Generar el título usando el modelo
        messages = [
            {"role": "user", "content": prompt}
        ]
        
        response = ""
        for message in client.chat_completion(
            messages=messages,
            max_tokens=max_length,
            temperature=0.7,
            stream=True
        ):
            token = message.choices[0].delta.content
            if token:
                response += token
        
        # Limpiar el título (quitar saltos de línea extra, etc.)
        title = response.strip().split("\\n")[0]
        
        return title
    
    except Exception as e:
        return f"Error: {str(e)}"

# Crear la interfaz de Gradio
with gr.Blocks(title="Title Generator with Llama 3.2") as demo:
    gr.Markdown("# 📝 AI Title Generator")
    gr.Markdown("Generate concise titles from text or conversation history using Llama 3.2-1B-Instruct")
    
    with gr.Tab("Text Input"):
        text_input = gr.Textbox(
            label="Enter your text",
            placeholder="Paste your text or conversation here...",
            lines=10
        )
        text_button = gr.Button("Generate Title", variant="primary")
        text_output = gr.Textbox(label="Generated Title", lines=2)
        
        text_button.click(
            fn=generate_title,
            inputs=[text_input],
            outputs=[text_output]
        )
    
    with gr.Tab("History/List Input"):
        gr.Markdown("Enter conversation history as JSON format:")
        gr.Markdown('Example: `[{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}]`')
        
        history_input = gr.Textbox(
            label="Conversation History (JSON)",
            placeholder='[{"role": "user", "content": "Your message here"}]',
            lines=10
        )
        history_button = gr.Button("Generate Title", variant="primary")
        history_output = gr.Textbox(label="Generated Title", lines=2)
        
        def process_history(history_json):
            try:
                import json
                history_list = json.loads(history_json)
                return generate_title(history_list)
            except json.JSONDecodeError:
                return "Error: Invalid JSON format"
        
        history_button.click(
            fn=process_history,
            inputs=[history_input],
            outputs=[history_output]
        )
    
    gr.Markdown("---")
    gr.Markdown("### API Usage")
    gr.Markdown("""
    You can use this API with CURL:
    
    ```bash
    curl -X POST "https://YOUR-SPACE-URL/call/generate_title" \\
      -H "Content-Type: application/json" \\
      -d '{"data": ["Your text here"]}'
    ```
    """)

# Lanzar la aplicación con API habilitada
if __name__ == "__main__":
    demo.launch(show_api=True)
'''

# Guardar el código en un archivo
with open('app.py', 'w', encoding='utf-8') as f:
    f.write(app_code)

# Crear el archivo requirements.txt
requirements = '''gradio>=4.0.0
huggingface_hub>=0.19.0
'''

with open('requirements.txt', 'w', encoding='utf-8') as f:
    f.write(requirements)

# Crear el README con instrucciones
readme = '''---
title: Title Generator with Llama 3.2
emoji: 📝
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
license: mit
---

# Title Generator with Llama 3.2-1B-Instruct

Generate concise titles from text or conversation history using Meta's Llama 3.2-1B-Instruct model.

## Features

- 📝 Generate titles from plain text
- 💬 Generate titles from conversation history
- 🚀 Fast inference with Llama 3.2-1B
- 🔌 RESTful API support for integration

## Setup

1. Go to your Space settings
2. Add a new secret: `HF_TOKEN` with your Hugging Face token
3. Make sure you have access to `meta-llama/Llama-3.2-1B-Instruct` (accept the gated model)

## API Usage

### CURL Example

```bash
curl -X POST "https://YOUR-SPACE-URL/call/generate_title" \\
  -H "Content-Type: application/json" \\
  -d '{"data": ["Your text or conversation here"]}'
```

### Python Example

```python
from gradio_client import Client

client = Client("YOUR-SPACE-URL")
result = client.predict("Your text here", api_name="/generate_title")
print(result)
```

## License

MIT License
'''

with open('README.md', 'w', encoding='utf-8') as f:
    f.write(readme)

print("✅ Archivos generados exitosamente:")
print("- app.py")
print("- requirements.txt") 
print("- README.md")
print("\n📦 Archivos listos para subir a Hugging Face Space")