harishreddy311 commited on
Commit
f450eae
·
1 Parent(s): 49d5aa6

Switch to Hugging Face dataset for persistent chat history

Browse files
Files changed (4) hide show
  1. .gradio/certificate.pem +31 -0
  2. app.py +18 -13
  3. chat_history.json +12 -0
  4. requirements.txt +4 -3
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
9
+ 0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
10
+ A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
11
+ T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
12
+ B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
13
+ B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJ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d11TPA
29
+ mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
app.py CHANGED
@@ -6,6 +6,7 @@ import logging
6
  from dotenv import load_dotenv
7
  import json
8
  from datetime import datetime
 
9
 
10
  # Load environment variables
11
  load_dotenv()
@@ -14,32 +15,36 @@ load_dotenv()
14
  logging.basicConfig(level=logging.INFO)
15
  logger = logging.getLogger(__name__)
16
 
17
- # File to store chat history
18
- CHAT_HISTORY_FILE = "chat_history.json"
 
19
 
20
  def load_chat_history():
21
  try:
22
- if os.path.exists(CHAT_HISTORY_FILE):
23
- with open(CHAT_HISTORY_FILE, 'r') as f:
24
- return json.load(f)
25
- return []
26
  except Exception as e:
27
- logger.error(f"Error loading chat history: {str(e)}")
28
  return []
29
 
30
  def save_chat_history(history):
31
  try:
32
- # Ensure the history file exists
33
- os.makedirs(os.path.dirname(CHAT_HISTORY_FILE) or '.', exist_ok=True)
34
- with open(CHAT_HISTORY_FILE, 'w') as f:
35
- json.dump(history, f, indent=2)
 
36
  except Exception as e:
37
  logger.error(f"Error saving chat history: {str(e)}")
38
 
39
  def clear_chat_history():
40
  try:
41
- if os.path.exists(CHAT_HISTORY_FILE):
42
- os.remove(CHAT_HISTORY_FILE)
 
 
 
43
  return "Chat history cleared successfully!"
44
  except Exception as e:
45
  logger.error(f"Error clearing chat history: {str(e)}")
 
6
  from dotenv import load_dotenv
7
  import json
8
  from datetime import datetime
9
+ from datasets import load_dataset, Dataset # type: ignore
10
 
11
  # Load environment variables
12
  load_dotenv()
 
15
  logging.basicConfig(level=logging.INFO)
16
  logger = logging.getLogger(__name__)
17
 
18
+ # Hugging Face dataset for chat history
19
+ DATASET_NAME = "harishreddy311/chat-history"
20
+ DATASET_SPLIT = "default"
21
 
22
  def load_chat_history():
23
  try:
24
+ dataset = load_dataset(DATASET_NAME, split=DATASET_SPLIT)
25
+ logger.info("Successfully loaded chat history from Hugging Face dataset")
26
+ return [dict(msg) for msg in dataset]
 
27
  except Exception as e:
28
+ logger.info("No existing chat history found, creating new dataset")
29
  return []
30
 
31
  def save_chat_history(history):
32
  try:
33
+ # Convert history to dataset format
34
+ dataset = Dataset.from_list(history)
35
+ # Push to Hugging Face
36
+ dataset.push_to_hub(DATASET_NAME, split=DATASET_SPLIT)
37
+ logger.info("Successfully saved chat history to Hugging Face dataset")
38
  except Exception as e:
39
  logger.error(f"Error saving chat history: {str(e)}")
40
 
41
  def clear_chat_history():
42
  try:
43
+ # Create empty dataset
44
+ empty_dataset = Dataset.from_list([])
45
+ # Push empty dataset to overwrite existing one
46
+ empty_dataset.push_to_hub(DATASET_NAME, split=DATASET_SPLIT)
47
+ logger.info("Successfully cleared chat history")
48
  return "Chat history cleared successfully!"
49
  except Exception as e:
50
  logger.error(f"Error clearing chat history: {str(e)}")
chat_history.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "role": "user",
4
+ "content": "Tell me about the causes of World War I",
5
+ "timestamp": "2025-04-19 19:26:42"
6
+ },
7
+ {
8
+ "role": "assistant",
9
+ "content": "The complex and multifaceted causes of World War I - a topic that has fascinated historians and scholars for decades. To understand the events that led to the Great War, let's start with the early 20th century, when the world was a vastly different place.\n\nImagine a delicate web of alliances, rivalries, and tensions between European powers, which had been building up over the years. This web was made up of several key threads:\n\n1. **Imperialism and Colonial Rivalries**: The late 19th and early 20th centuries saw a scramble for colonies and resources around the world. European powers like Britain, France, Germany, Belgium, and Italy were competing for control over territories in Africa and Asia. This competition fueled tensions and rivalries between nations.\n2. **Nationalism**: As nations began to assert their identities and interests, nationalism grew, and with it, a sense of patriotism and pride. This led to an increase in military spending and a buildup of armed forces, which in turn created an atmosphere of militarism.\n3. **Militarism**: The early 20th century saw a significant increase in military spending and a buildup of armed forces across Europe. This created an atmosphere of tension and competition, where nations felt the need to demonstrate their military strength.\n4. **Alliances**: To balance power and protect their interests, European nations formed complex alliances. The two main alliances were:\n\t* The Triple Entente: France, Britain, and Russia.\n\t* The Triple Alliance: Germany, Austria-Hungary, and Italy.\n5. **The Balkans**: The Balkan region was a powder keg of tensions, with various ethnic groups and nations vying for control. The Ottoman Empire, which had once dominated the region, was weakening, and this created a power vacuum that European powers sought to fill.\n\nNow, let's talk about the specific events that led to the outbreak of World War I:\n\n* **The Assassination of Archduke Franz Ferdinand** (June 28, 1914): In Sarajevo, Bosnia, Gavrilo Princip, a Serbian nationalist, assassinated Archduke Franz Ferdinand, the heir to the throne of Austria-Hungary. This event sparked a chain reaction of diplomatic crises and military mobilizations.\n* **Austria-Hungary's Ultimatum to Serbia** (July 23, 1914): In response to the assassination, Austria-Hungary issued an ultimatum to Serbia, which Serbia refused to comply with. This led to Austria-Hungary declaring war on Serbia on July 28, 1914.\n* **The Complex System of Alliances**: As Austria-Hungary declared war on Serbia, Russia began to mobilize its military in support of Serbia. Germany, allied with Austria-Hungary, declared war on Russia on August 1, 1914, and then declared war on France on August 3, 1914.\n* **Britain's Entry into the War** (August 4, 1914): When Germany invaded Belgium, a neutral country, Britain declared war on Germany, citing its commitment to defend Belgium's neutrality.\n\nThe complex interplay of these factors created a situation in which a small spark, like the assassination of Archduke Franz Ferdinand, could ignite a global conflict. The war would go on to claim millions of lives, ravage entire cities, and reshape the world order.\n\nTo further explore this fascinating topic, I'd like to ask: What do you think was the most significant factor contributing to the outbreak of World War I? Was it the complex system of alliances, the rise of nationalism, or something else entirely?\n\nSome related topics to explore:\n\n* The Treaty of Versailles and its impact on the interwar period\n* The role of the United States in World War I\n* The rise of fascist and nationalist movements in the aftermath of the war\n\nI hope this sparks your interest in the complex and intriguing history of World War I!",
10
+ "timestamp": "2025-04-19 19:26:42"
11
+ }
12
+ ]
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
- gradio>=4.0.0,<5.0.0
2
- groq>=0.4.0
3
- python-dotenv>=1.0.0
 
 
1
+ gradio==4.19.2
2
+ groq==0.4.2
3
+ python-dotenv==1.0.1
4
+ datasets==2.19.1