mlfonso18 commited on
Commit
49ef84b
·
verified ·
1 Parent(s): d5c1002

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+
5
+ HF_TOKEN = os.environ.get("HF_TOKEN")
6
+
7
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
8
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
9
+
10
+ def generar_texto(prompt):
11
+ if not prompt.strip():
12
+ return "Escribe una pregunta."
13
+
14
+ payload = {
15
+ "inputs": f"[INST] {prompt} [/INST]",
16
+ "parameters": {
17
+ "max_new_tokens": 200,
18
+ "temperature": 0.7,
19
+ "top_p": 0.9,
20
+ "return_full_text": False
21
+ }
22
+ }
23
+
24
+ r = requests.post(API_URL, headers=HEADERS, json=payload)
25
+
26
+ if r.status_code != 200:
27
+ return "Error del modelo, intenta más tarde."
28
+
29
+ return r.json()[0]["generated_text"]
30
+
31
+ demo = gr.Interface(
32
+ fn=generar_texto,
33
+ inputs=gr.Textbox(label="Pregunta"),
34
+ outputs=gr.Textbox(label="Respuesta"),
35
+ title="IA con Mistral (Gratis)"
36
+ )
37
+
38
+ demo.launch()
39
+