Cristobal299 commited on
Commit
e1d773b
·
verified ·
1 Parent(s): 188e1be

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -6,25 +6,26 @@ import requests
6
  import gradio as gr
7
 
8
  # ----------------------------------------------------------------------
9
- # Configuracion de la API
10
  # ----------------------------------------------------------------------
11
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
12
  GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
13
  MODEL_NAME = os.environ.get("MODEL_NAME", "llama-3.1-8b-instant")
14
 
15
  # ----------------------------------------------------------------------
16
- # CSS para la aplicacion (se pasa a demo.launch)
17
  # ----------------------------------------------------------------------
18
  CSS = """
19
- #business-input textarea {
20
- border: 1px solid #CCCCCC;
21
- }
22
- #business-input textarea:focus {
23
- border-color: #1E90FF;
24
  }
25
  #generate-btn {
26
  background-color: #1E90FF;
27
  color: white;
 
 
 
 
28
  }
29
  #generate-btn:hover {
30
  background-color: #1C86EE;
@@ -32,16 +33,27 @@ CSS = """
32
  """
33
 
34
  # ----------------------------------------------------------------------
35
- # Utilidades
36
  # ----------------------------------------------------------------------
37
- def extract_json(text: str) -> dict:
38
  """
39
- Extrae el primer bloque JSON valido del texto recibido.
40
  """
41
- start = text.find("{")
42
- end = text.rfind("}")
43
- if start == -1 or end == -1:
44
- raise ValueError("No JSON found in response")
45
- json_str = text[start:end + 1]
46
- # Elimina bloques de codigo markdown si existen
47
- json_str = re.sub(r"^
 
 
 
 
 
 
 
 
 
 
 
 
6
  import gradio as gr
7
 
8
  # ----------------------------------------------------------------------
9
+ # Configuration
10
  # ----------------------------------------------------------------------
11
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
12
  GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
13
  MODEL_NAME = os.environ.get("MODEL_NAME", "llama-3.1-8b-instant")
14
 
15
  # ----------------------------------------------------------------------
16
+ # CSS (passed to demo.launch)
17
  # ----------------------------------------------------------------------
18
  CSS = """
19
+ #chatbot .message {
20
+ font-family: Arial, Helvetica, sans-serif;
 
 
 
21
  }
22
  #generate-btn {
23
  background-color: #1E90FF;
24
  color: white;
25
+ border: none;
26
+ padding: 0.5rem 1rem;
27
+ font-size: 1rem;
28
+ cursor: pointer;
29
  }
30
  #generate-btn:hover {
31
  background-color: #1C86EE;
 
33
  """
34
 
35
  # ----------------------------------------------------------------------
36
+ # Helper to call Groq API
37
  # ----------------------------------------------------------------------
38
+ def call_groq(messages):
39
  """
40
+ Sends a chat completion request to Groq and returns the assistant's reply.
41
  """
42
+ if not GROQ_API_KEY:
43
+ raise RuntimeError("GROQ_API_KEY not set in environment.")
44
+ headers = {
45
+ "Authorization": f"Bearer {GROQ_API_KEY}",
46
+ "Content-Type": "application/json"
47
+ }
48
+ payload = {
49
+ "model": MODEL_NAME,
50
+ "messages": messages,
51
+ "temperature": 0.7
52
+ }
53
+ response = requests.post(GROQ_ENDPOINT, headers=headers, json=payload, timeout=30)
54
+ response.raise_for_status()
55
+ data = response.json()
56
+ # Extract the assistant's content
57
+ content = data["choices"][0]["message"]["content"]
58
+ # Some models wrap the answer in markdown code fences; clean them
59
+ if content.startswith("