Futuresony commited on
Commit
8bb7427
·
verified ·
1 Parent(s): 6a0afb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -7
app.py CHANGED
@@ -3,6 +3,11 @@ from huggingface_hub import InferenceClient
3
  from textblob import TextBlob
4
  import json
5
  import os
 
 
 
 
 
6
 
7
  # Get the API token from the environment variable
8
  api_token = os.getenv('HUGGINGFACEHUB_API_TOKEN')
@@ -70,12 +75,23 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, fe
70
 
71
  formatted_prompt = format_alpaca_prompt(message, system_message, history)
72
 
73
- response = client.text_generation(
74
- formatted_prompt,
75
- max_new_tokens=max_tokens,
76
- temperature=temperature,
77
- top_p=top_p,
78
- )
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # ✅ Extract only the response
81
  cleaned_response = response.split("### Response:")[-1].strip()
@@ -90,6 +106,22 @@ def collect_feedback(response, feedback):
90
  """Collect user feedback on the chatbot's response"""
91
  save_interaction(response, feedback=feedback)
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  feedback_interface = gr.Interface(
94
  fn=collect_feedback,
95
  inputs=[
@@ -112,4 +144,5 @@ demo = gr.ChatInterface(
112
 
113
  if __name__ == "__main__":
114
  demo.launch()
115
- feedback_interface.launch()
 
 
3
  from textblob import TextBlob
4
  import json
5
  import os
6
+ import time
7
+ import logging
8
+
9
+ # Set up logging
10
+ logging.basicConfig(level=logging.INFO)
11
 
12
  # Get the API token from the environment variable
13
  api_token = os.getenv('HUGGINGFACEHUB_API_TOKEN')
 
75
 
76
  formatted_prompt = format_alpaca_prompt(message, system_message, history)
77
 
78
+ # Retry mechanism
79
+ max_retries = 3
80
+ for attempt in range(max_retries):
81
+ try:
82
+ response = client.text_generation(
83
+ formatted_prompt,
84
+ max_new_tokens=max_tokens,
85
+ temperature=temperature,
86
+ top_p=top_p,
87
+ )
88
+ break # Exit the loop if the request is successful
89
+ except Exception as e:
90
+ logging.error(f"Attempt {attempt + 1} failed: {e}")
91
+ if attempt < max_retries - 1:
92
+ time.sleep(2 ** attempt) # Exponential backoff
93
+ else:
94
+ raise e
95
 
96
  # ✅ Extract only the response
97
  cleaned_response = response.split("### Response:")[-1].strip()
 
106
  """Collect user feedback on the chatbot's response"""
107
  save_interaction(response, feedback=feedback)
108
 
109
+ def view_interactions():
110
+ if os.path.exists(INTERACTIONS_FILE):
111
+ with open(INTERACTIONS_FILE, "r") as file:
112
+ interactions = json.load(file)
113
+ return json.dumps(interactions, indent=4)
114
+ else:
115
+ return "No interactions found."
116
+
117
+ # Create a Gradio interface to display interactions
118
+ view_interface = gr.Interface(
119
+ fn=view_interactions,
120
+ inputs=[],
121
+ outputs="text",
122
+ title="View Interactions"
123
+ )
124
+
125
  feedback_interface = gr.Interface(
126
  fn=collect_feedback,
127
  inputs=[
 
144
 
145
  if __name__ == "__main__":
146
  demo.launch()
147
+ feedback_interface.launch()
148
+ view_interface.launch()