mpolanco commited on
Commit
55b21de
1 Parent(s): c55bdd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -32
app.py CHANGED
@@ -1,38 +1,55 @@
1
  import streamlit as st
2
  import requests
3
- import json
4
-
5
- def get_spell_correction(input_text):
6
- response = requests.post(
7
- "https://api.respell.ai/v1/run",
8
- headers={
9
- "Authorization": "Bearer 260cee54-6d54-48ba-92e8-bf641b5f4805",
10
- "Accept": "application/json",
11
- "Content-Type": "application/json"
12
- },
13
- data=json.dumps({
14
- "spellId": "1th5Ic7ZnMmZXBtZCfR7-",
15
- "spellVersionId": "-NTOo8UsyhC-3g52p7ExZ",
16
- "inputs": {
17
- "pregunta": input_text,
18
- }
19
- }),
20
- )
21
-
22
- try:
23
- corrected_text = response.json()["results"]["corrections"]["pregunta"]["text"]
24
- return corrected_text
25
- except KeyError:
26
- st.error("Error: Unable to extract corrected text from the API response.")
27
- return None
28
 
29
- st.title("Respell Spell Checker")
 
 
 
 
 
 
 
 
 
 
30
 
31
- input_text = st.text_area("Enter your text here:")
32
 
33
- if st.button("Check Spelling"):
34
- corrected_text = get_spell_correction(input_text)
 
35
 
36
- if corrected_text is not None:
37
- st.write("Corrected Text:")
38
- st.write(corrected_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import requests
3
+ import os
4
+ import re
5
+
6
+ # Reemplaza "tu_clave_de_api_aqui" con tu clave de API real
7
+ API_KEY = "fcbfdfe8-e9ed-41f3-a7d8-b6587538e84e"
8
+
9
+ def get_api_response(question):
10
+ url = "https://api.afforai.com/api/api_completion"
11
+ payload = {
12
+ "apiKey": API_KEY,
13
+ "sessionID": "65489d7c9ad727940f2ab26f",
14
+ "history": [{"role": "user", "content": question}],
15
+ "powerful": True,
16
+ "google": True
17
+ }
18
+
19
+ response = requests.post(url, json=payload)
20
+ return response.json()
 
 
 
 
 
 
 
21
 
22
+ def extract_source(completion):
23
+ # Busca el patrón 【3†source】 y extrae la información de la fuente
24
+ source_pattern = "【\\d+†source】"
25
+ source_match = re.search(source_pattern, completion)
26
+
27
+ if source_match:
28
+ source_text = source_match.group(0)
29
+ source_number = re.search(r"\d+", source_text).group(0)
30
+ source_name = f"NombreFuente{source_number}"
31
+ source_url = f"https://URLFuente{source_number}.com"
32
+ return source_name, source_url, source_number # Agregado source_number
33
 
34
+ return None, None, None
35
 
36
+ def main():
37
+ st.title("Preguntas sobre las leyes de Guatemala")
38
+ question = st.text_input("Escribe tu pregunta")
39
 
40
+ if st.button("Enviar"):
41
+ response = get_api_response(question)
42
+ completion = response.get('output', {}).get('completion', '')
43
+
44
+ # Extraer información de la fuente
45
+ source_name, source_url, source_number = extract_source(completion) # Agregado source_number
46
+
47
+ # Reemplazar la cadena en el texto de completitud
48
+ if source_name and source_url:
49
+ completion = completion.replace(f"【{source_number}†source】", f"Fuente: [{source_name}]({source_url})")
50
+
51
+ st.markdown(completion)
52
+
53
+ if __name__ == "__main__":
54
+ main()
55
+