dwfefef commited on
Commit
7fe21e8
·
verified ·
1 Parent(s): aff9237

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -39
app.py CHANGED
@@ -1,15 +1,7 @@
1
  import logging
2
  from flask import Flask, render_template, request, jsonify
3
  import requests
4
- from fastapi import FastAPI, HTTPException
5
- from pydantic import BaseModel
6
- from typing import List
7
- from gradio_client import Client
8
  import os
9
- import nest_asyncio
10
-
11
- nest_asyncio.apply()
12
-
13
 
14
  app = Flask(__name__)
15
  logging.basicConfig(level=logging.DEBUG)
@@ -27,8 +19,6 @@ class ChatContext:
27
  return "\n".join([f"{msg['role']}: {msg['content']}" for msg in self.messages])
28
 
29
  chat_context = ChatContext()
30
- # Initialize the client (this loads the API)
31
- client = Client(os.getenv("CLIENT_URL"))
32
 
33
  # System message
34
  system_message = """You are Echo 1.5, an AI assistant developed by Abhinav. Your purpose is to assist users with their queries and provide helpful information. You should always be polite, respectful, and informative in your responses.
@@ -45,26 +35,25 @@ Iamwomen: iamwomen.cloudns.be
45
  Iamwomen_Instagram: https://www.instagram.com/i_am_woman.24/
46
  """
47
 
48
- # Define the input model
49
- class ChatInput(BaseModel):
50
- messages: List[dict]
51
-
52
  def chat_with_ai(context):
53
  # Prepare the full prompt
54
- #full_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in context])
55
-
56
  # Make the API call
57
- result = client.predict(
58
- message=full_prompt,
59
- request=os.getenv('MODEL'),
60
- param_3=0.5,
61
- param_4=4096,
62
- param_5=0.5,
63
- param_6=0,
64
- api_name="/chat"
65
- )
 
 
 
 
66
 
67
- return result
68
  @app.route('/')
69
  def index():
70
  return render_template('index.html')
@@ -83,20 +72,13 @@ def chat():
83
 
84
  app.logger.debug(f"Full context: {full_context}")
85
 
 
 
 
86
  try:
87
- response = chat_with_ai(context=full_context)
88
- )
89
- app.logger.debug(f"API response status: {response.status_code}")
90
- app.logger.debug(f"API response content: {response.text}")
91
- response.raise_for_status()
92
- ai_response = response.json()["response"]
93
- return jsonify({"response": ai_response})
94
- except requests.RequestException as e:
95
- app.logger.error(f"Error communicating with API: {str(e)}")
96
- return jsonify({"error": f"Error communicating with API: {str(e)}"}), 500
97
- except KeyError as e:
98
- app.logger.error(f"Unexpected response format: {response.text}")
99
- return jsonify({"error": f"Unexpected response format: {str(e)}"}), 500
100
  except Exception as e:
101
  app.logger.error(f"Unexpected error: {str(e)}")
102
  return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500
 
1
  import logging
2
  from flask import Flask, render_template, request, jsonify
3
  import requests
 
 
 
 
4
  import os
 
 
 
 
5
 
6
  app = Flask(__name__)
7
  logging.basicConfig(level=logging.DEBUG)
 
19
  return "\n".join([f"{msg['role']}: {msg['content']}" for msg in self.messages])
20
 
21
  chat_context = ChatContext()
 
 
22
 
23
  # System message
24
  system_message = """You are Echo 1.5, an AI assistant developed by Abhinav. Your purpose is to assist users with their queries and provide helpful information. You should always be polite, respectful, and informative in your responses.
 
35
  Iamwomen_Instagram: https://www.instagram.com/i_am_woman.24/
36
  """
37
 
 
 
 
 
38
  def chat_with_ai(context):
39
  # Prepare the full prompt
40
+ full_prompt = context.get_context()
41
+
42
  # Make the API call
43
+ try:
44
+ response = requests.post(API_URL, json={"prompt": full_prompt})
45
+ response.raise_for_status()
46
+ return response.json()["response"]
47
+ except requests.RequestException as e:
48
+ app.logger.error(f"Error communicating with API: {str(e)}")
49
+ raise Exception(f"Error communicating with API: {str(e)}")
50
+ except KeyError as e:
51
+ app.logger.error(f"Unexpected response format: {response.text}")
52
+ raise Exception(f"Unexpected response format: {str(e)}")
53
+ except Exception as e:
54
+ app.logger.error(f"Unexpected error: {str(e)}")
55
+ raise Exception(f"An unexpected error occurred: {str(e)}")
56
 
 
57
  @app.route('/')
58
  def index():
59
  return render_template('index.html')
 
72
 
73
  app.logger.debug(f"Full context: {full_context}")
74
 
75
+ chat_context.messages = conversation_history
76
+ chat_context.add_message('user', user_input)
77
+
78
  try:
79
+ response = chat_with_ai(chat_context)
80
+ app.logger.debug(f"API response content: {response}")
81
+ return jsonify({"response": response})
 
 
 
 
 
 
 
 
 
 
82
  except Exception as e:
83
  app.logger.error(f"Unexpected error: {str(e)}")
84
  return jsonify({"error": f"An unexpected error occurred: {str(e)}"}), 500