JairoCesar commited on
Commit
e0ecd33
verified
1 Parent(s): 59f83ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -2
app.py CHANGED
@@ -1,5 +1,9 @@
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
 
 
 
 
3
 
4
  # Initialize the client
5
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
@@ -35,6 +39,32 @@ def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, r
35
  output += response.token.text
36
  return output
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Streamlit interface
39
  st.title("Interpretaci贸n del Test de Rorschach")
40
 
@@ -43,14 +73,26 @@ if 'history' not in st.session_state:
43
  st.session_state.history = []
44
 
45
  # User input
46
- entrada_usuario = st.text_input("Que ves en las imagenes ..:", key="entrada_usuario")
47
 
48
- # Generate response and update history
49
  if st.button("Enviar"):
50
  if entrada_usuario:
51
  bot_response = generate(entrada_usuario, st.session_state.history)
52
  st.session_state.history.append((entrada_usuario, bot_response))
53
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Display conversation
55
  chat_text = ""
56
  for user_msg, bot_msg in st.session_state.history:
 
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
3
+ from docx import Document
4
+ import tempfile
5
+ import os
6
+ import re
7
 
8
  # Initialize the client
9
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
39
  output += response.token.text
40
  return output
41
 
42
+ # Function to replace variables in a Word template
43
+ def replace_variables_word(doc, variables):
44
+ for paragraph in doc.paragraphs:
45
+ for key, value in variables.items():
46
+ if f'<{key}>' in paragraph.text:
47
+ paragraph.text = re.sub(f'<{key}>', value, paragraph.text)
48
+ for table in doc.tables:
49
+ for row in table.rows:
50
+ for cell in row.cells:
51
+ for paragraph in cell.paragraphs:
52
+ for key, value in variables.items():
53
+ if f'<{key}>' in paragraph.text:
54
+ paragraph.text = re.sub(f'<{key}>', value, paragraph.text)
55
+
56
+ # Function to generate a Word document with interpretation
57
+ def generate_word_document(interpretation):
58
+ template_path = os.path.join('PLANTILLAS', 'PLANTILLA_INTERPRETACION.docx')
59
+ doc = Document(template_path)
60
+
61
+ variables = {'INTERPRETACION': interpretation}
62
+ replace_variables_word(doc, variables)
63
+
64
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as tmp:
65
+ doc.save(tmp.name)
66
+ return tmp.name
67
+
68
  # Streamlit interface
69
  st.title("Interpretaci贸n del Test de Rorschach")
70
 
 
73
  st.session_state.history = []
74
 
75
  # User input
76
+ entrada_usuario = st.text_input("Que ves en las im谩genes ..:", key="entrada_usuario")
77
 
78
+ # Generate response and create Word document
79
  if st.button("Enviar"):
80
  if entrada_usuario:
81
  bot_response = generate(entrada_usuario, st.session_state.history)
82
  st.session_state.history.append((entrada_usuario, bot_response))
83
 
84
+ # Generate Word document
85
+ document_path = generate_word_document(bot_response)
86
+
87
+ # Provide download link
88
+ with open(document_path, "rb") as file:
89
+ st.download_button(
90
+ label="Descargar Interpretaci贸n",
91
+ data=file,
92
+ file_name="Interpretacion_Rorschach.docx",
93
+ mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
94
+ )
95
+
96
  # Display conversation
97
  chat_text = ""
98
  for user_msg, bot_msg in st.session_state.history: